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 ...
随机推荐
- Android动态添加Device Admin权限
/********************************************************************** * Android动态添加Device Admin权限 ...
- linux shell 中文件编码查看及转换方法
参考: http://edyfox.codecarver.org/html/vim_fileencodings_detection.html 一.查看文件编码. 在打开文件的时候输入:set ...
- 九度OJ1205题-递归求解问题
题目1205:N阶楼梯上楼问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:5887 解决:2446 题目描述: N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用 ...
- [LeetCode&Python] Problem 206. Reverse Linked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- easyui的DataGrid的单元格添加ProgressBar进度条
网上的搜到的好多不能用,官方easy-ui使用进度条 <div id="p" class="easyui-progressbar" ></di ...
- lesson8-图像问答-小象cv
QA即图像问答:覆盖最全面的AI,ai完备性 动态模型:不同任务需要不同模型 or 不同细分任务需要不同模型参数 数据集: 1)VQA,显示图片+抽象场景:每个问题给10个不同答案:含有无图片答案(考 ...
- 代码basic讲解
key1 import os g = os.walk(r'D:\Users\Quincy_C\PycharmProjects\S6')print(next(g))print(next(g)) 第一次n ...
- 无用之flask
Oldboy s4 Flask Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收 ...
- [ZOJ 4062][2018ICPC青岛站][Plants vs. Zombies]
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062 题目大意:给一个大小为n的数组,数组编号从1到n,每一个元素的值代表 ...
- [zoj4046][树状数组求逆序(强化版)]
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4046 题意:有一个含有n个元素的数列p,每个元素均不同且为1~n中的一个, ...