线程间通信-示例
来源:互联网 发布:maya软件配置要求 编辑:程序博客网 时间:2024/06/15 11:17
package com.huaWei.threadCommunication;
/**
* 线程间通信资源类
* 线程间通信:多个线程在处理同一资源,但是任务却不同。
*
* @author 华伟科技
*
*/
public class Resource {
private String name;
private String sex;
private boolean flag = false;
// 同步的好处:解决了线程的安全问题。
// 同步的弊端:相对降低了效率,因为同步外的线程都会判断同步锁。
// 同步的前提:必须有多个线程并使用同一个锁。
// 等待/唤醒机制,涉及的方法:
// (1)wait:让线程处于等待状态,被wait的线程会被存储在线程池中。
// (2)notify:唤醒线程池中一个线程(任意)。
// (3)notifyAll:唤醒线程池中所有的线程。
// 这些方法都必须定义在同步中,因为这些方法是用于操作线程状态的方法,必须要明确到底操作的是哪个锁上的线程。
// 为什么操作线程的方法wait、notify、notifyAll定义在Object类中?
// (1)因为这些方法是监视器的方法,监视器其实就是锁。
// (2)锁可以是任意的对象,任意的对象调用的方式一定定义在Object类中。
public synchronized void set(String name, String sex) {
if (flag)
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
}
public synchronized void out() {
if (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(name + "......." + sex);
flag = false;
this.notify();
}
}
package com.huaWei.threadCommunication;
/**
* 输入线程
*
* @author 华伟科技
*
*/
public class Input implements Runnable {
private Resource resource;
public Input(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (true) {
if (i == 0) {
resource.set("mike", "man");
} else {
resource.set("丽丽", "女女女女女女女女女女女");
}
i = (i + 1) % 2;
}
}
}
package com.huaWei.threadCommunication;
/**
* 输出线程
*
* @author 华伟科技
*
*/
public class Output implements Runnable {
private Resource resource;
public Output(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
resource.out();
}
}
}
package com.huaWei.threadCommunication;
/**
* 线程间通信测试类
*
* @author 华伟科技
*
*/
public class ThreadCommunicationMain {
public static void main(String[] args) {
threadCommunication();
}
public static void threadCommunication() {
// 创建资源
Resource resource = new Resource();
// 创建任务
Input input = new Input(resource);
Output output = new Output(resource);
// 创建线程,执行路径
Thread thread1 = new Thread(input);
Thread thread2 = new Thread(output);
// 开启线程
thread1.start();
thread2.start();
}
}
- 线程间通信-示例
- 线程间通信示例
- 线程间通信示例1
- 23-多线程(线程间通信-示例)
- JavaSE 多线程 线程间通信示例-1
- python 线程间通信方法一:event 示例
- 线程通信示例:循环打印AB10次
- java多线程-线程间通信-示例代码-解决安全问题-等待唤醒机制wait()notify()notifyAll()
- iOS 【Multithreading-线程间的交互通信(图片下载示例)(掌握)】
- 毕向东讲解(摘)—11.多线程线程间通信(示例代码)
- 线程同步--线程间通信
- 线程同步--线程间通信
- JAVASE线程---线程间通信
- JAVA线程-线程间通信
- 线程3:线程间通信
- 线程与线程间通信
- 线程间通信
- c#线程间通信
- noip2011 瑞士轮 (归并排序)
- WinHttpOpenRequest第6个参数如何传?LPCWSTR*
- 朴素贝叶斯
- 连接 数据库
- leetcode:Longest Valid Parentheses 使用动态规划O(n)思路
- 线程间通信-示例
- Scala深入浅出进阶经典 第87讲:Scala中使用For表达式做查询
- Eclipse 配置tomcat8
- epoll简介(一)
- string 和strngbuffer间的相互转换
- Scala深入浅出进阶经典 第88讲:Scala中使用For表达式实现map、flatMap、filter
- 排序算法四:交换排序之冒泡排序
- HTTP中的Cookie
- 第九章使用ContentProvider实现数据共享