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. Java中的面向对象I

    一.首先来了解一下Java面向对象的五个程序设计方式: 1.万物皆对象 Java以类为基本模块来将问题抽象化,在计算机中解决实际生活中的问题 2.程序为对象的集合,程序中的类通过互发消息来告知彼此要做 ...

  2. pandas Dataframe 构造

  3. XML中DTD,XSD的区别与应用

    XML我们并不陌生,在企业级应用中有很广的用途.具体就不再说,下面介绍一下DTD,XSD的区别并以XSD为例看spring中定义与使用.1.DTD(Documnet Type Definition)D ...

  4. 对Functional Language的认识

    What: A functional language is a programming language built over and around logical functions or pro ...

  5. jquery选择后代以及toggle,toggleClass用法

    $("#id").find(".classname")      选择id为xx下的类名为xx的元素 toggle  $(this).next(".c ...

  6. (18)模型层 -ORM之msql 多表操作(字段的属性)

    数据库表的对应关系 1.一对一   #关联字段写在那张表都可以 PS:只要写OneToOneField就会自动加一个id 2.一对多  #关系确立,关联字段写在多的一方 3.多对多   #多对多的关系 ...

  7. Introducing Makisu: Uber’s Fast, Reliable Docker Image Builder for Apache Mesos and Kubernetes

    转自:https://eng.uber.com/makisu/?amp To ensure the stable, scalable growth of our diverse tech stack, ...

  8. DevExpress控件使用方法:第一篇 gridControl详解

    GridControl (1)层次设计器 有五种视图模式,banded gridview多行表头,数据还是一行一组,最靠近数据的表头与数据一一对应:advanced banded gridview多行 ...

  9. 日志插件 log4net 的配置和使用

    文本格式说明 可以记载的日志类别包括:FATAL(致命错误).ERROR(一般错误).WARN(警告).INFO(一般信息).DEBUG(调试信息). 文本参数说明 %m(message):输出的日志 ...

  10. 电脑上不安装Oracle时,C# 调用oracle数据库,Oracle客户工具 【转载】

    http://www.cnblogs.com/jiekzou/p/5047850.html Oracle的安装包通常都比较大,安装又比较费时,而且如果安装过程中不幸出错,各种蛋疼,即便是安装过N遍的老 ...