博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
两个线程交替打印字符串
阅读量:7080 次
发布时间:2019-06-28

本文共 3763 字,大约阅读时间需要 12 分钟。

每个对象都有一内置锁

wait方法 释放对象锁(不占对象锁)

sleep方法不释放对象锁(占对象锁)

优秀写法  (下面写法可能有问题,synchronized (LOCK)  提到 while前面就好了)

1 class Info { 2     String printStr = "i think this is ok"; 3     int i = 0; 4  5     public void print() { 6         if (i < printStr.length()) { 7             System.out.println(Thread.currentThread().getName() + "  print   " 8                     + printStr.charAt(i)); 9             i++;10         }11     }12 }13 14 public class main {15     private static Object LOCK = new Object();16     static Info info = new Info(); // 互斥资源17     static boolean flag = false; // false for a ,true for b18 19     public static void main(String[] args) {20         // 两个线程 交替打印字符串21         Thread a = new Thread() {22             public void run() {23                 while (info.i < info.printStr.length())24                     synchronized (LOCK) {25                         {26                             if (false == flag) {27                                 try {28                                     LOCK.wait();// 在wait后的瞬间线程b得到锁29                                 } catch (InterruptedException e) {30                                     e.printStackTrace();31                                 }32                             }33                             flag = false;34                             info.print();35                             LOCK.notify();// 在这里虽然唤醒了另一个线程b,但锁并没有释放36                         }37                     }38             };39         };40         Thread b = new Thread() {41             public void run() {42                 while (info.i < info.printStr.length())43                     synchronized (LOCK) {44                         {45                             if (true == flag) {46                                 try {47                                     LOCK.wait();// 在wait后的瞬间线程b得到锁48                                 } catch (InterruptedException e) {49                                     e.printStackTrace();50                                 }51                             }52                             flag = true;53                             info.print();54                             LOCK.notify();// 在这里虽然唤醒了另一个线程b,但锁并没有释放55                         }56                     }57             };58         };59         a.start();60         b.start();61     }62 63 }

 

代码1

1 package ThreadTest; 2  3  4 class Info 5 { 6     String printStr="i think this is ok"; 7     int i=0; 8     boolean flag=false; 9     public  void print1()10     {11         synchronized(this)12         {13             if(flag==false)14             {15                 try {16                     this.wait();17                 } catch (InterruptedException e) {18                     e.printStackTrace();19                 }20             }21         realprint();22         flag=false;23         notify();24         }25     }26     public  void print2()27     {28         synchronized(this)29         {30             if(flag==true)31             {32                 try {33                     this.wait();34                 } catch (InterruptedException e) {35                     e.printStackTrace();36                 }37             }38         realprint();39         flag=true;40         notify();41         }42     }43     public void realprint()44     {45         if(i

代码2

1 package MyThreadMethod;  2   3 import java.util.concurrent.locks.Condition;  4 import java.util.concurrent.locks.ReentrantLock;  5   6 class Info  7 {  8     String printStr="i think this is ok";  9     int i=0; 10     public  void print1() 11     { 12         if(i

 

3 不同写法

 

1 class Info 2 { 3     String printStr="i think this is ok"; 4     int i=0; 5     public void print() 6     { 7         if(i

 

转载于:https://www.cnblogs.com/friends-wf/p/3658258.html

你可能感兴趣的文章
Yii 框架之采用自带的jquery库实现ajax分页
查看>>
linux命令:系统裁剪之一grub引导 复制库文件脚本 % #截取目录的部分
查看>>
Spring Boot通过JdbcTemplate进行数据持久化
查看>>
eclipse中修改JSP模板中的默认编码
查看>>
《转》OpenStack组件keystone与LDAP协议集成(OpenLDAP和Active Directory)
查看>>
《3》CentOS7.0+OpenStack+kvm云平台部署—配置Glance
查看>>
Python学习笔记(2)-python对象
查看>>
oracle常用语句
查看>>
动态调用动态语言之Java脚本API
查看>>
Input自动补全,并且将输入的数据存放到本地
查看>>
Linux CentOS PhpMyAdmin安装
查看>>
腾云驾物—使用Azure IoT Hub接收树莓派传感数据展现到Power BI
查看>>
节能罩
查看>>
web前端小工具
查看>>
ListView美化(1)-android:descendantFocusability用法简析
查看>>
在Windows上安装Elasticsearch-6.3.2
查看>>
分布式存储ceph监控calamari安装
查看>>
深度剖析——超融合架构应用与实践分享
查看>>
重命名文件
查看>>
学习pyqt 基础 3
查看>>