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线程之间的通信的更多相关文章

  1. Java线程之间通信

    用多线程的目的:更好的利用CPU的资源.因为所有的多线程代码都可以用单线程来实现. 多线程:指的是这个程序(一个进程)运行时产生了不止一个线程. 并行:多个CPU实例或者多台机器同时执行一段处理逻辑, ...

  2. Java多线程编程-线程之间的通信

    转载自:这里 学习了基础的线程知识 看到了 线程之间的通信 线程之间有哪些通信方式呢? 1.同步 这里讲的同步是指多个线程通过synchronized关键字这种方式来实现线程间的通信. public ...

  3. java之线程(线程的创建方式、java中的Thread类、线程的同步、线程的生命周期、线程之间的通信)

    CPU:10核 主频100MHz 1核  主频    3GHz 那么哪一个CPU比较好呢? CPU核不是越多越好吗?并不一定.主频用于衡量GPU处理速度的快慢,举个例子10头牛运送货物快还是1架飞机运 ...

  4. java并发学习--第六章 线程之间的通信

    一.等待通知机制wait()与notify() 在线程中除了线程同步机制外,还有一个最重要的机制就是线程之间的协调任务.比如说最常见的生产者与消费者模式,很明显如果要实现这个模式,我们需要创建两个线程 ...

  5. Java线程——线程之间的通信

    Java中多线程间的通信是怎么实现的? 线程通信的方式: (1)共享变量 线程之间的通信可以通过发送信号,发送信号的一个简单方法就是再共享的对象里面设置信号值.线程A在一个同步块中设置boolean型 ...

  6. Thread线程源码解析,Java线程的状态,线程之间的通信

    线程的基本概念 什么是线程 现代操作系统在运行一个程序的时候,会为其创建一个进程.例如,启动一个Java程序,操作系统就会创建一个Java进程.线代操作系统调度的最小单位是线程.也叫做轻量级进程.在一 ...

  7. Java学习笔记46(多线程三:线程之间的通信)

    多个线程在处理同一个资源,但是线程的任务却不相同,通过一定的手段使各个线程能有效地利用资源, 这种手段即:等待唤醒机制,又称作线程之间的通信 涉及到的方法:wait(),notify() 示例: 两个 ...

  8. 基础学习day12--多线程一线程之间的通信和常用方法

    一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信.    等待/唤醒机制涉及的方法:    1. wait():让线程处于冻结状态,被wa ...

  9. Android笔记(三十一)Android中线程之间的通信(三)子线程给主线程发送消息

    先看简单示例:点击按钮,2s之后,TextView改变内容. package cn.lixyz.handlertest; import android.app.Activity; import and ...

随机推荐

  1. Unescaped left brace in regex is illegal here in regex; marked by <-- HERE in m/\${ <-- HERE ([^ \t=:+{}]+)}/ at xxxx/usr/bin/automake line 3939.

    /********************************************************************** * Unescaped left brace in re ...

  2. Cython 使用

    链接: Cython是一个快速生成Python扩展模块的工具,从语法层面上来讲是Python语法和C语言语法的混血,当Python性能遇到瓶颈时,Cython直接将C的原生速度植入Python程序,这 ...

  3. Unity 3D学习心得,程序员开发心得分享!

    Unity开发之路 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创新! ...

  4. XXS level5

    (1)用第四关的方法尝试,发现不行,查看页面源代码,发现on中间有了下划线 (2)查看PHP源代码 <?php ini_set("display_errors", 0); $ ...

  5. Readability Assessment for Text Simplification -paper

    https://pdfs.semanticscholar.org/e43a/3c3c032cf3c70875c4193f8f8818531857b2.pdf 1.introduction在Brazil ...

  6. 软件产品案例分析——福州大学微信小程序

    一 .调研,评测 评测 第一次上手体验: 刚进入看到菜单界面,感觉还是比较生动清晰的,功能很多,也很全面,包涵了大部分学生所需要的功能,就是第一次身份验证那里找了半天. bug: 1.点击进入学生证附 ...

  7. canvas的认识,时钟的设置

    canvas的三要素:ID标识,width宽度,height高度,他是行元素 IE9才支持canvas,canvas是一个透明的画板,要用js去画 绘制一个圆 线性渐变颜色 径向渐变 图片的绘制: 视 ...

  8. Spring Boot 揭秘

    SpringBoot基础 微服务 解决大一统的服务化架构的问题 代码冲突问题 交付复杂,影响面大 测试困难 微服务的好处 可扩展性 隔离性 灵活性,多语言多技术生态 微服务的挑战 保持微服务的互通性 ...

  9. Myelipse中xml约束文件的导入(以spring为例)

    为了在电脑处于未联网状态下,beans.xml中书写标签具有提示功能,需要在电脑本地导入约束文件,下面上图 注意:将location后缀添加到key中beans的后面 注意:导入 context,ao ...

  10. 【BZOJ3238】【AHOI2013】差异

    sam好,好写好调好ac! 原题: 图片题面好评 2<=N<=500000 在syq大神的指点下终于理解一道后缀自动姬了quq (其实是因为这道题的dp主要是在后缀树(就是拓扑序)上搞树形 ...