【java】-- 多线程之间实现通讯
1、多线程之间如何实现通讯
1.1、什么是多线程之间通讯?
多线程之间通讯,其实就是多个线程在操作同一个资源,但是操作的动作不同。
画图演示
1.2、多线程之间通讯需求
需求:第一个线程写入(input)用户,另一个线程取读取(out)用户.实现读一个,写一个操作。
2、代码实现基本实现
2.1、共享资源源实体类
|
class Res { public String userSex; public String userName; } |
输入线程资源
|
class IntThrad extends Thread { private Res res; public IntThrad(Res res) { this.res = res; } @Override public void run() { int count = 0; while (true) { if (count == 0) { res.userName = "余胜军"; res.userSex = "男"; } else { res.userName = "小紅"; res.userSex = "女"; } count = (count + 1) % 2; } } } |
输出线程
|
class OutThread extends Thread { private Res res;
public OutThread(Res res) { this.res = res; }
@Override public void run() { while (true) { System.out.println(res.userName + "--" + res.userSex); } } } |
运行代码
|
Res res = new Res(); IntThrad intThrad = new IntThrad(res); OutThread outThread = new OutThread(res); intThrad.start(); outThread.start(); |
运行代码
注意:数据发生错乱,造成线程安全问题
解决线程安全问题
IntThrad 加上synchronized
|
class IntThrad extends Thread { private Res res; public IntThrad(Res res) { this.res = res; } @Override public void run() { int count = 0; while (true) { synchronized (res) { if (count == 0) { res.userName = "余胜军"; res.userSex = "男"; } else { res.userName = "小紅"; res.userSex = "女"; } count = (count + 1) % 2; } } } } |
输出线程加上synchronized
|
class Res { public String userName; public String sex; }
class InputThread extends Thread { private Res res;
public InputThread(Res res) { this.res = res; }
@Override public void run() { int count = 0; while (true) { synchronized (res) { if (count == 0) { res.userName = "余胜军"; res.sex = "男"; } else { res.userName = "小红"; res.sex = "女"; } count = (count + 1) % 2; }
} } }
class OutThrad extends Thread { private Res res;
public OutThrad(Res res) { this.res = res; }
@Override public void run() { while (true) { synchronized (res) { System.out.println(res.userName + "," + res.sex); } }
} }
public class ThreadDemo01 {
public static void main(String[] args) { Res res = new Res(); InputThread inputThread = new InputThread(res); OutThrad outThrad = new OutThrad(res); inputThread.start(); outThrad.start(); }
}
|
wait()、notify、notifyAll()方法
wait()、notify()、notifyAll()是三个定义在Object类里的方法,可以用来控制线程的状态。
这三个方法最终调用的都是jvm级的native方法。随着jvm运行平台的不同可能有些许差异。
如果对象调用了wait方法就会使持有该对象的线程把该对象的控制权交出去,然后处于等待状态。
如果对象调用了notify方法就会通知某个正在等待这个对象的控制权的线程可以继续运行。
如果对象调用了notifyAll方法就会通知所有等待这个对象控制权的线程继续运行。
注意:一定要在线程同步中使用,并且是同一个锁的资源
|
class Res { public String userSex; public String userName; //线程通讯标识 public boolean flag = false; }
|
|
class IntThrad extends Thread { private Res res; public IntThrad(Res res) { this.res = res; } @Override public void run() { int count = 0; while (true) { synchronized (res) { if (res.flag) { try { // 当前线程变为等待,但是可以释放锁 res.wait(); } catch (Exception e) { } } if (count == 0) { res.userName = "余胜军"; res.userSex = "男"; } else { res.userName = "小紅"; res.userSex = "女"; } count = (count + 1) % 2; res.flag = true; // 唤醒当前线程 res.notify(); } } } } |
|
class OutThread extends Thread { private Res res; public OutThread(Res res) { this.res = res; } @Override public void run() { while (true) { synchronized (res) { if (!res.flag) { try { res.wait(); } catch (Exception e) { // TODO: handle exception } } System.out.println(res.userName + "--" + res.userSex); res.flag = false; res.notify(); } } } } |
|
public class ThreaCommun { public static void main(String[] args) { Res res = new Res(); IntThrad intThrad = new IntThrad(res); OutThread outThread = new OutThread(res); intThrad.start(); outThread.start(); } } |
wait与sleep区别?
对于sleep()方法,我们首先要知道该方法是属于Thread类中的。而wait()方法,则是属于Object类中的。
sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。
在调用sleep()方法的过程中,线程不会释放对象锁。
而当调用wait()方法的时候,线程会放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象调用notify()方法后本线程才进入对象锁定池准备
获取对象锁进入运行状态。
JDK1.5-Lock
在 jdk1.5 之后,并发包中新增了 Lock 接口(以及相关实现类)用来实现锁功能,Lock 接口提供了与 synchronized 关键字类似的同步功能,但需要在使用时手动获取锁和释放锁。
Lock写法
|
Lock lock = new ReentrantLock(); lock.lock(); try{ //可能会出现线程安全的操作 }finally{ //一定在finally中释放锁 //也不能把获取锁在try中进行,因为有可能在获取锁的时候抛出异常 lock.ublock(); } |
Lock 接口与 synchronized 关键字的区别
Lock 接口可以尝试非阻塞地获取锁 当前线程尝试获取锁。如果这一时刻锁没有被其他线程获取到,则成功获取并持有锁。
Lock 接口能被中断地获取锁 与 synchronized 不同,获取到锁的线程能够响应中断,当获取到的锁的线程被中断时,中断异常将会被抛出,同时锁会被释放。
Lock 接口在指定的截止时间之前获取锁,如果截止时间到了依旧无法获取锁,则返回。
Condition用法
Condition的功能类似于在传统的线程技术中的,Object.wait()和Object.notify()的功能。
代码
|
Condition condition = lock.newCondition(); res. condition.await(); 类似wait res. Condition. Signal() 类似notify |
|
class Res { public String userName; public String sex; public boolean flag = false; Lock } class InputThread extends Thread { private Res res; Condition public InputThread(Res res, Condition newCondition) { this.res = res; this.newCondition=newCondition; } @Override public void run() { int count = 0; while (true) { // synchronized (res) { try { res.lock.lock(); if (res.flag) { try { // res.wait(); newCondition.await(); } // TODO: handle exception } } if (count == 0) { res.userName = "余胜军"; res.sex = "男"; } res.userName = "小红"; res.sex = "女"; } count = (count + 1) % 2; res.flag = true; // res.notify(); newCondition.signal(); } // TODO: handle exception }finally { res.lock.unlock(); } } // } } } class OutThrad extends Thread { private Res res; private Condition newCondition; public OutThrad(Res res,Condition newCondition) { this.res = res; this.newCondition=newCondition; } @Override public void run() { while (true) { // synchronized (res) { try { res.lock.lock(); if (!res.flag) { try { // res.wait(); newCondition.await(); } // TODO: handle exception } } System.out.println(res.userName + "," + res.sex); res.flag = false; // res.notify(); newCondition.signal(); } // TODO: handle exception }finally { res.lock.unlock(); } // } } } } public class ThreadDemo01 { public static void main(String[] args) { Res Condition InputThread OutThrad inputThread.start(); outThrad.start(); } } |
如何停止线程?
停止线程思路
1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。
2. 使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)。
3. 使用interrupt方法中断线程。
代码:
|
class StopThread implements Runnable { private boolean flag = true;
@Override public synchronized void run() { while (flag) { try { wait(); } catch (Exception e) { //e.printStackTrace(); stopThread(); } System.out.println("thread run.."); } } /** * * * * * * * */ public void stopThread() { flag = false; } } /** * * @classDesc: 功能描述:(停止线程) * @author: 余胜军 * @createTime: 2017年8月20日 下午8:05:25 * @version: v1.0 * @copyright:上海每特教育科技有限公司 */ public class StopThreadDemo {
public static void main(String[] args) { StopThread stopThread1 = new StopThread(); Thread thread1 = new Thread(stopThread1); Thread thread2 = new Thread(stopThread1); thread1.start(); thread2.start(); int i = 0; while (true) { System.out.println("thread main.."); if (i == 300) { // stopThread1.stopThread(); thread1.interrupt(); thread2.interrupt(); break; } i++; }
}
}
|
ThreadLoca
什么是ThreadLoca
ThreadLocal提高一个线程的局部变量,访问某个线程拥有自己局部变量。
当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。
ThreadLocal的接口方法
ThreadLocal类接口很简单,只有4个方法,我们先来了解一下:
- void
set(Object value)设置当前线程的线程局部变量的值。
- public
Object get()该方法返回当前线程所对应的线程局部变量。
- public
void remove()将当前线程局部变量的值删除,目的是为了减少内存的占用,该方法是JDK
5.0新增的方法。需要指出的是,当线程结束后,对应该线程的局部变量将自动被垃圾回收,所以显式调用该方法清除线程的局部变量并不是必须的操作,但它可以加快内存回收的速度。
- protected
Object initialValue()返回该线程局部变量的初始值,该方法是一个protected的方法,显然是为了让子类覆盖而设计的。这个方法是一个延迟调用方法,在线程第1次调用get()或set(Object)时才执行,并且仅执行1次。ThreadLocal中的缺省实现直接返回一个null。
案例:创建三个线程,每个线程生成自己独立序列号。
代码:
|
class Res { // 生成序列号共享变量 public static Integer count = 0; public static protected Integer
return 0; };
};
public Integer getNum() { int count = threadLocal.get() + 1; threadLocal.set(count); return count; } }
public class ThreadLocaDemo2 extends Thread { private Res res;
public ThreadLocaDemo2(Res res) { this.res = res; }
@Override public void run() { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() }
}
public static void main(String[] args) { Res res = new Res(); ThreadLocaDemo2 threadLocaDemo1 = new ThreadLocaDemo2(res); ThreadLocaDemo2 threadLocaDemo2 = new ThreadLocaDemo2(res); ThreadLocaDemo2 threadLocaDemo3 = new ThreadLocaDemo2(res); threadLocaDemo1.start(); threadLocaDemo2.start(); threadLocaDemo3.start(); }
} |
ThreadLoca实现原理
ThreadLoca通过map集合
Map.put(“当前线程”,值);
【java】-- 多线程之间实现通讯的更多相关文章
- python处理多线程之间事件通讯方法
一.什么是事件 每执行一个事情,肯定有该事情的执行后状态,那事件就是该事情发生的信号 在程序中,多线程之间需要通讯,而事件就是方便线程之间的通讯 案例: 1.服务器启动需要5秒 2.客服端启动后去链接 ...
- java 中多线程之间的通讯之生产者和消费者 (多个线程之间的通讯)
在真实开发 中关于多线程的通讯的问题用到下边的例子是比较多的 不同的地方时if 和while 的区别 如果只是两个线程之间的通讯,使用if是没有问题的. 但是在多个线程之间就会有问题 /* * 这个例 ...
- Java 多线程间的通讯
在前一小节,介绍了在多线程编程中使用同步机制的重要性,并学会了如何实现同步的方法来正确地访问共享资源.这些线程之间的关系是平等的,彼此之间并不存在任何依赖,它们各自竞争CPU资源,互不相让,并且还无条 ...
- java多线程之间的顺序问题
java 多线程: 这样写有问题的:这样写可以的: package com.test; import java.util.concurrent.CountDownLatch; import java. ...
- Java 线程之间的通讯,等待唤醒机制
public class ThreadNotifySample { public static void main(String[] args) { // Res res = new Res(); / ...
- java 中多线程之间的通讯之等待唤醒机制
wait notify () nitifyAll () 都使用在同步中,因为要对持有监视器(锁)的线程操作 所以要使用在同步中,因为只有同步才具有锁 为什么这些操作线程的方法要定义object类中呢 ...
- JAVA多线程之间共享数据BlockingQueue介绍
在JAVA的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利. ...
- Java 多线程Socket编程通讯--实现聊天室代码
1.创建服务器类 import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import ja ...
- java多线程之间的通信
目的 如何让两个线程依次执行? 那如何让 两个线程按照指定方式有序交叉运行呢? 四个线程 A B C D,其中 D 要等到 A B C 全执行完毕后才执行,而且 A B C 是同步运行的 三个运动员各 ...
随机推荐
- ACM-ICPC 2018 沈阳赛区网络预赛 F Fantastic Graph(贪心或有源汇上下界网络流)
https://nanti.jisuanke.com/t/31447 题意 一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间 分析 最大流不太会.. ...
- <HTML>初识HTML
最近在阅读Head first HTML and CSS, 写一些笔记. 小知识: 1. 浏览器会忽略HTML文档中的制表符,回车和大部分空格——要用标记 2. WYSIWYG——使得用户在视图中 ...
- mesbox公告加更新控制
0为不显示,1为显示~~~~0|友情提示:任何时候,不要相信福利软件,福利网站,不乱接收别人发的的任何文件,如需使用军旗有关产品,请至官方群或官方网站下载!!|183|173~~~~162,1651, ...
- Excel表格中依据某一列的值,将这列中一样的数据放在一个文件中。
一需求:按照标题C的内容,一样的数据整理到一个文件中. 二.操作: 1.atl+F11弹出vb窗口 2.点击 插入===>模块 ,复制以下代码,注意这是一个表头为三行的函数(保存 ...
- combine_lat_dirs.sh
#!/bin/bash # Copyright 2018 Jarvan Wang Apache 2.0. # Copyright 2016 Xiaohui Zhang Apache 2.0. ...
- net core appsetting配置
public class BaseController : Controller { protected WLEntity _db; protected ILogger _log; protected ...
- python2 线程基础
1,感谢菜鸟教程, 线程基础:导入,创建函数,创建线和运行 import thread import time # 为线程定义一个函数 def print_time(threadName, delay ...
- iOS 开发 ZFUI framework控件,使布局更简单
来自:http://www.jianshu.com/p/bcf86b170d9c 前言 为什么会写这个?因为在iOS开发中,界面的布局一直没有Android布局有那么多的方法和优势,我个人开发都是纯代 ...
- imageview设置图片时超长超大图片超出限制(OpenGLRenderer: Bitmap too large to be uploaded into a texture (996x9116, max=4096x4096))
问题:遇到超长图片,宽长等比缩放,比如宽度同屏幕同宽,长度等比放大,放到后遇到长度超出OpenGLRenderer的最大限制,导致图片无法显示出来: 解决办法: //图片超出GPU对于openglRe ...
- Sping 里面的适配器模式的实现
适配器模式----------设计模式最近在看SpringMVC源码,从中看到了比较优秀的设计模式所以来分享下. 1.适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口,Adap ...