Thread--两线程交替打印
1 package t3.copy; 2 3 public class ThreadA extends Thread { 4 5 private Object lock; 6 7 public ThreadA(Object lock) { 8 super(); 9 this.lock = lock; 10 } 11 12 @Override 13 public void run() { 14 try { 15 while(true) { 16 synchronized (lock) { 17 if(Run.flag) { 18 System.out.println(Thread.currentThread().getName() + " ~~~~ "); 19 Run.flag = !Run.flag; 20 lock.wait(); 21 } 22 } 23 } 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 } 28 29 30 31 }
1 package t3.copy; 2 3 public class ThreadB extends Thread { 4 5 private Object lock; 6 7 public ThreadB(Object lock) { 8 super(); 9 this.lock = lock; 10 } 11 12 @Override 13 public void run() { 14 try { 15 while(true) { 16 synchronized (lock) { 17 if(!Run.flag) { 18 System.out.println(Thread.currentThread().getName() + " !!!! "); 19 Run.flag = !Run.flag; 20 lock.notify(); 21 } 22 } 23 24 } 25 } catch (Exception e) { 26 e.printStackTrace(); 27 } 28 } 29 30 31 32 }
1 package t3.copy; 2 3 public class Run { 4 5 public static boolean flag = true; 6 7 public static void main(String[] args) { 8 try { 9 Object lock = new Object(); 10 ThreadA a = new ThreadA(lock); 11 a.start(); 12 Thread.sleep(50); 13 ThreadB b = new ThreadB(lock); 14 b.start(); 15 } catch (InterruptedException e) { 16 e.printStackTrace(); 17 } 18 } 19 }
不知道可不可以不借助于中间变量判断。
不借助于中间变量判断,但是交替打印次数一致。
1 package conditionTest.copy; 2 3 import java.util.concurrent.locks.Condition; 4 import java.util.concurrent.locks.ReentrantLock; 5 6 public class MyService { 7 8 private ReentrantLock lock = new ReentrantLock(); 9 private Condition conditionSet = lock.newCondition(); 10 private Condition conditionGet = lock.newCondition(); 11 12 public void set() { 13 try { 14 lock.lock(); 15 // while(hasValue) { 16 // } 17 System.out.println("打印★★★★★★"); 18 conditionGet.signal(); 19 conditionSet.await(); 20 } catch (Exception e) { 21 // TODO: handle exception 22 e.printStackTrace(); 23 } finally { 24 lock.unlock(); 25 } 26 } 27 28 public void get() { 29 try { 30 lock.lock(); 31 System.out.println("打印☆☆☆☆☆☆"); 32 conditionSet.signal(); 33 conditionGet.await(); 34 } catch (Exception e) { 35 // TODO: handle exception 36 e.printStackTrace(); 37 } finally { 38 lock.unlock(); 39 } 40 } 41 42 }
1 package conditionTest.copy; 2 3 public class MyThreadA extends Thread { 4 5 private MyService service; 6 7 public MyThreadA(MyService service) { 8 super(); 9 this.service = service; 10 } 11 12 @Override 13 public void run() { 14 for(int i=0; i<100; i++) { 15 service.set(); 16 } 17 } 18 19 20 21 }
1 package conditionTest.copy; 2 3 public class MyThreadB extends Thread { 4 5 private MyService service; 6 7 public MyThreadB(MyService service) { 8 super(); 9 this.service = service; 10 } 11 12 @Override 13 public void run() { 14 for(int i=0; i<100; i++) { 15 service.get(); 16 } 17 } 18 19 20 21 }
1 package conditionTest.copy; 2 3 public class Run { 4 5 public static void main(String[] args) { 6 MyService service = new MyService(); 7 MyThreadA a = new MyThreadA(service); 8 a.start(); 9 MyThreadB b = new MyThreadB(service); 10 b.start(); 11 } 12 13 }