zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    2018最新php笔试题及答案(持续更新)
    快速上手模板制作
    春节期间小游戏同时在线人数最高达2800万人/小时
    公众平台新增修改文章错别字功能 每篇文章允许被修改一次仅限正文内五个字
    微信6.6.2版更新:支持两个账号一键切换
    小程序支持打开APP了 还有小程序的标题栏也可以自定义
    小程序发布重磅数据:日活跃用户数1.7亿、已上线小程序58万个,覆盖100万开发者、2300个第三方平台
    张小龙2018PRO版微信公开课演讲全文 透露2018微信全新计划
    除了跳一跳还有16款微信小游戏可以玩
    小游戏里潜藏着600亿的大市场
  • 原文地址:https://www.cnblogs.com/microcat/p/6364760.html
Copyright © 2011-2022 走看看