java线程之间的通信
1.常用的方法
sleep() 该线程进入等待状态,不释放锁
wait() 该线程进入等待状态,释放锁
notify() 随机唤醒一个线程
notifyAll() 唤醒全部线程
getName() 获取线程对象的名称。默认情况下,名字的组成 Thread-编号(编号从0开始)
setName(String name) 设置线程名称
currentThread() 返回当前正在执行的线程对象引用
sleep(Long time) 让当前线程休眠time毫秒
setDaemon(boolean on) 设置线程为守护线程,一旦前台(主线程),结束,守护线程就结束了
join() 当前线程暂停, 等待指定的线程执行结束后, 当前线程再继续
join(int time) 当前线程暂停, 等待指定的线程执行time秒结束后, 当前线程再继续
yield() 暂停当前正在执行的线程对象,并执行其他线程。
getPriority() 获取线程优先级
setPriority(int newPriority)更改线程的优先级
2.线程之间的通信
a.两个线程之间的通信
public class ThreadExchange {
@Test
public void test2Thread() {
MyPrint myPrint = new MyPrint();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
myPrint.printA();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
myPrint.printB();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
class MyPrint {
private Integer flag = 0;
public synchronized void printA() throws InterruptedException {
if (flag != 0) {
this.wait();
}
System.out.print("a");
System.out.print("a");
System.out.print("a");
System.out.print("a");
System.out.println("a");
flag = 1;
this.notify();
}
public synchronized void printB() throws InterruptedException {
if (flag != 1) {
this.wait();
}
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.println("b");
flag = 0;
this.notify();
}
}
b.三个以上的线程之间的通信
方式一
public class ThreadExchange {
@Test
public void test2Thread() {
MyPrint myPrint = new MyPrint();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
myPrint.printA();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
myPrint.printB();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
class MyPrint {
private Integer flag = 0;
public synchronized void printA() throws InterruptedException {
if (flag != 0) {
this.wait();
}
System.out.print("a");
System.out.print("a");
System.out.print("a");
System.out.print("a");
System.out.println("a");
flag = 1;
this.notify();
}
public synchronized void printB() throws InterruptedException {
if (flag != 1) {
this.wait();
}
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.println("b");
flag = 0;
this.notify();
}
}
2.三个以上的线程之间的通信
public class ThreadExchange {
@Test
public void test2Thread() {
final MyPrint p = new MyPrint();
new Thread() {
public void run() {
while (true) {
try {
p.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while (true) {
try {
p.print2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while (true) {
try {
p.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class MyPrint {
private int flag = 1;
public void print1() throws InterruptedException {
synchronized(this) {
while(flag != 1) {
this.wait();
}
System.out.print("a");
System.out.print("a");
System.out.print("a");
System.out.print("a");
System.out.println("a");
flag = 2;
this.notifyAll();
}
}
public void print2() throws InterruptedException {
synchronized (this) {
while (flag != 2) {
this.wait();
}
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.println("b");
flag = 3;
this.notifyAll();
}
}
public void print3() throws InterruptedException {
synchronized (this) {
while (flag != 3) {
this.wait();
}
System.out.print("c");
System.out.print("c");
System.out.print("c");
System.out.print("c");
System.out.println("c");
flag = 1;
this.notifyAll();
}
}
}
方式二
public class ThreadExchange {
@Test
public void test3Thread() {
final MyPrint p = new MyPrint();
new Thread() {
public void run() {
while (true) {
try {
p.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while (true) {
try {
p.print2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while (true) {
try {
p.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class MyPrint {
private ReentrantLock r = new ReentrantLock();
private Condition c1 = r.newCondition();
private Condition c2 = r.newCondition();
private Condition c3 = r.newCondition();
private int flag = 1;
public void print1() throws InterruptedException {
r.lock(); // 获取锁
if (flag != 1) {
c1.await();
}
System.out.print("a");
System.out.print("a");
System.out.print("a");
System.out.print("a");
System.out.println("a");
flag = 2;
c2.signal();
r.unlock(); // 释放锁
}
public void print2() throws InterruptedException {
r.lock();
if (flag != 2) {
c2.await();
}
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.println("b");
flag = 3;
c3.signal();
r.unlock();
}
public void print3() throws InterruptedException {
r.lock();
if (flag != 3) {
c3.await();
}
System.out.print("c");
System.out.print("c");
System.out.print("c");
System.out.print("c");
System.out.println("c");
flag = 1;
c1.signal();
r.unlock();
}
}
java线程之间的通信的更多相关文章
- Java线程之间通信
用多线程的目的:更好的利用CPU的资源.因为所有的多线程代码都可以用单线程来实现. 多线程:指的是这个程序(一个进程)运行时产生了不止一个线程. 并行:多个CPU实例或者多台机器同时执行一段处理逻辑, ...
- Java多线程编程-线程之间的通信
转载自:这里 学习了基础的线程知识 看到了 线程之间的通信 线程之间有哪些通信方式呢? 1.同步 这里讲的同步是指多个线程通过synchronized关键字这种方式来实现线程间的通信. public ...
- java之线程(线程的创建方式、java中的Thread类、线程的同步、线程的生命周期、线程之间的通信)
CPU:10核 主频100MHz 1核 主频 3GHz 那么哪一个CPU比较好呢? CPU核不是越多越好吗?并不一定.主频用于衡量GPU处理速度的快慢,举个例子10头牛运送货物快还是1架飞机运 ...
- java并发学习--第六章 线程之间的通信
一.等待通知机制wait()与notify() 在线程中除了线程同步机制外,还有一个最重要的机制就是线程之间的协调任务.比如说最常见的生产者与消费者模式,很明显如果要实现这个模式,我们需要创建两个线程 ...
- Java线程——线程之间的通信
Java中多线程间的通信是怎么实现的? 线程通信的方式: (1)共享变量 线程之间的通信可以通过发送信号,发送信号的一个简单方法就是再共享的对象里面设置信号值.线程A在一个同步块中设置boolean型 ...
- Thread线程源码解析,Java线程的状态,线程之间的通信
线程的基本概念 什么是线程 现代操作系统在运行一个程序的时候,会为其创建一个进程.例如,启动一个Java程序,操作系统就会创建一个Java进程.线代操作系统调度的最小单位是线程.也叫做轻量级进程.在一 ...
- Java学习笔记46(多线程三:线程之间的通信)
多个线程在处理同一个资源,但是线程的任务却不相同,通过一定的手段使各个线程能有效地利用资源, 这种手段即:等待唤醒机制,又称作线程之间的通信 涉及到的方法:wait(),notify() 示例: 两个 ...
- 基础学习day12--多线程一线程之间的通信和常用方法
一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信. 等待/唤醒机制涉及的方法: 1. wait():让线程处于冻结状态,被wa ...
- Android笔记(三十一)Android中线程之间的通信(三)子线程给主线程发送消息
先看简单示例:点击按钮,2s之后,TextView改变内容. package cn.lixyz.handlertest; import android.app.Activity; import and ...
随机推荐
- 爬取贴吧中的html,并保存到相对应的文件夹中
功能:输入要爬取的贴吧名称,起始页和终止页即可. # -*- coding: utf-8 -*- import urllib.request import urllib.parse import os ...
- jQuery事件委托方法 bind live delegate on
1.bind jquery 1.3之前 定义和用法:主要用于给选择到的元素上绑定特定事件类型的监听函数 语法: bind(type,[data],function(e)); 特点: a.适合页 ...
- tomcat服务器安装方法
tomcat: 链接:https://pan.baidu.com/s/1pMEu0hP 密码:g0ah (tomcat7) jdk :链接:https://pan.baidu.com/s/1 ...
- Python基础练习及答案
1.请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li=['alex', 'eric', 'rain'] 该题目主要是考的字符串的拼接,join方法, s = "" li ...
- python使用dns轮循检测web服务器是否异常
我使用的是python2.7,我本来另装了一个python3.6,发现无法安装dnspython,于是只能换回来了 import dns.resolver #这个需要另外下载并安装(下载地址www.d ...
- redis使用问题总结
1.redis使用过多内存导致其他进程无法正常运行情况: 解决方案:限制redis的最大使用内存,修改redis.conf中的maxmemory(一般不要超过空闲内存的3/5,如果不设置ma ...
- Bat相关的项目应用
原 bat 命令如何启动远程PC上的一个程序? 原 bat 自动解压缩,发布asp.net程序 原 bat 自动更新代码,编译,压缩asp.net程序 原 bat自动备份压缩文件 原 bat命令ora ...
- linux修改文件所有者和文件所在组 【转载】
chgrp 用户名 文件名 -R chown 用户名 文件名 -R -R表示递归目录下所有文件 以上部分已验证 地址原贴
- AI update
1, labeling工具 - 测试完成 使用fiji + Alps_Labeling_Tool.ijm 做labeling 生成的文件可以使用python读取 2,training -未开始 使用t ...
- Dynamic Signals and Slots
Ref https://doc.qt.io/archives/qq/qq16-dynamicqobject.html Trolltech | Documentation | Qt Quarterly ...