如何控制线程的运行次序

51 阅读1分钟

固定运行顺序

wait和notify

@Slf4j(topic = "c.Main")
public class Main {
   static final Object lock = new Object();
   //表示t2是否运行过
   static boolean t2runned = false;
   public static void main(String[] args) {
      Thread t1 = new Thread(() -> {
         synchronized (lock) {
            while (!t2runned){
               try {
                  lock.wait();
               } catch (InterruptedException e) {
                  throw new RuntimeException(e);
               }
            }
         }
         log.debug("1");
      }, "t1");
      Thread t2 = new Thread(() -> {
         synchronized (lock) {
            log.debug("2");
            t2runned = true;
            lock.notifyAll();
         }
      }, "t1");
      t1.start();
      t2.start();
   }

}

park和unpark

@Slf4j(topic = "c.Main")
public class Main {
	public static void main(String[] args) {
		Thread t1 = new Thread(() -> {
			LockSupport.park();
			log.debug("1");
		}, "t1");
		Thread t2 = new Thread(() -> {
			log.debug("2");
			LockSupport.unpark(t1);
		}, "t2");
		t1.start();
		t2.start();
	}
}

交替输出

线程1输出a5次,线程2输出b5次,线程3输出c5次。要求输出abcabcabcabcabc如何实现

wait和notify

@Slf4j(topic = "c.Main")
public class Main {

   private static final Object lock = new Object();
   private static Integer loopNumber = 5;
   private static int flag = 1;
   public static void main(String[] args) {
      Thread t1 = new Thread(() -> {
         printf("a",1,2);
      }, "t1");
      Thread t2 = new Thread(() -> {
         printf("b",2,3);
      }, "t2");
      Thread t3 = new Thread(() -> {
         printf("c",3,1);

      }, "t3");
      t1.start();
      t2.start();
      t3.start();
   }

   private static void printf(String printTag, int tag, int last) {
      for (Integer integer = 0; integer < loopNumber; integer++) {
         synchronized (lock) {
            while (flag != tag){
               try {
                  lock.wait();
               } catch (InterruptedException e) {
                  throw new RuntimeException(e);
               }
            }
            if (flag == tag){
               System.out.print(printTag);
               flag = last;
               lock.notifyAll();
            }
         }
      }
   }
}

await与signal

@Slf4j(topic = "c.Main")
public class Main {
   private static ReentrantLock lock = new ReentrantLock();
   private static Condition a = lock.newCondition();
   private static Condition b = lock.newCondition();
   private static Condition c = lock.newCondition();
   private static Integer loopNumber = 5;
   public static void main(String[] args) {
      Thread t1 = new Thread(() -> {
         print("a",a,b);
      },"t1");
      Thread t2 = new Thread(() -> {
         print("b",b,c);
      },"t2");
      Thread t3 = new Thread(() -> {
         print("c",c,a);
      },"t3");
      t1.start();
      t2.start();
      t3.start();
      try {
         Thread.sleep(10);
         lock.lock();
         a.signal();
         lock.unlock();
      } catch (InterruptedException e) {
         throw new RuntimeException(e);
      }
   }
   private static void print(String printTag, Condition tag, Condition last){
      for (Integer i = 0; i < loopNumber; i++) {
         lock.lock();
         try {
            tag.await();
         } catch (InterruptedException e) {
            throw new RuntimeException(e);
         }
         System.out.print(printTag);
         last.signal();
         lock.unlock();
      }
   }
}

park和unpark

@Slf4j(topic = "c.Main")
public class Main {

   private static Integer loopNumber = 5;

   static Thread t1,t2,t3;
   public static void main(String[] args) throws InterruptedException {

      t1 = new Thread(() -> {
         print("a",t2);
      },"t1");
      t2 = new Thread(() -> {
         print("b",t3);
      },"t2");
      t3 = new Thread(() -> {
         print("c",t1);
      },"t3");
      t1.start();
      t2.start();
      t3.start();
      Thread.sleep(10);
      LockSupport.unpark(t1);
   }

   private static void print(String printTag, Thread last){
      for (Integer i = 0; i < loopNumber; i++) {
         LockSupport.park();
         System.out.print(printTag);
         LockSupport.unpark(last);
      }
   }
}