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. RNN-LSTM-GRU-BIRNN

    https://blog.csdn.net/wangyangzhizhou/article/details/76651116 共三篇 RNN的模型展开后多个时刻隐层互相连接,而所有循环神经网络都有一个 ...

  2. jqGrid 加载完jqGrid之后可以执行函数的方法

    , gridComplete: function() { jQuery('#first_gridpager').html("首页 "); jQuery('#prev_gridpag ...

  3. chmod、chown、umask、lsattr/chattr

    1.chmod 命令 改变文件权限 文件对于使用者来说,有 读 .写 .执行 (当然,还有删除),而这里主要说的是,读写执行(rwx)    r  w  x    对应的是 读写执行,也对应 :  4 ...

  4. C与C++中实现 gotoxy()函数

    #include <stdio.h> #include <windows.h> void gotoxy(int x, int y) { COORD pos = {x,y}; H ...

  5. HDU 2036 叉乘求三角形面积

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

  6. day11hadoop高可用和Hive

    PS:视频一直就是在演示   高可用(比较偏运维一点) PS:Active是对外提供服务的,standBy是从属备用的:但是他们是怎样保证同步的数据的呢?一个运行中zookeeper上的第三方那个工具 ...

  7. 生产环境部署MongoDB副本集(带keyfile安全认证以及用户权限)

    本文同步于个人Github博客:https://github.com/johnnian/Blog/issues/8,欢迎留言. 安装软件包:mongodb-linux-x86_64-3.4.1.tgz ...

  8. Linux内核启动分析笔记

    一.驱动加载 1.驱动加载调用关系 start_kernel //init/main.c rest_init //最后执行它 kernel_init //使用kernel_thread创建一个进程执行 ...

  9. Go程序语言设计 (艾伦 A. A. 多诺万 著)

    第1章 入门  (已看) 1.1 hello,world package main import "fmt" func main(){ fmt.Println("Hell ...

  10. Python网络_TCP/IP简介

    本章将介绍tcp网络编程,更多内容请参考:Python学习指南 Socket是网络编程的一个抽象概念,通常我们用一个Socket表示"打开了一个网络连接",而打开一个Socket需 ...