标签:boolean strong handle out 注意 机制 sleep 指定 读取
/**
* 测试等待/通知机制
* @author hao
*
*/
public class Test_NoWaitNotify {
public static void main(String[] args) {
Res res = new Res();
IntThrad intThrad = new IntThrad(res);
OutThread outThread = new OutThread(res);
intThrad.start();
outThread.start();
}
}
/*
* 共享资源源实体类
*
*/
class Res {
public String userSex;
public String userName;
}
/*
*
* 输入线程资源
*
*/
class IntThrad extends Thread {
private Res res;
public IntThrad(Res res) {
this.res = res;
}
@Override
public void run() {
int count = 0;
while (true) {
if (count == 0) {
res.userName = "小明";
res.userSex = "男";
} else {
res.userName = "小红";
res.userSex = "女";
}
count = (count + 1) % 2;
}
}
}
/*
*
* 输出线程
*
*/
class OutThread extends Thread {
private Res res;
public OutThread(Res res) {
this.res = res;
}
@Override
public void run() {
while (true) {
System.out.println(res.userName + "--" + res.userSex);
}
}
}
加入synchronized 关键字
public class Test002 {
public static void main(String[] args) {
Res2 res = new Res2();
IntThrad2 intThrad = new IntThrad2(res);
OutThread2 outThread = new OutThread2(res);
intThrad.start();
outThread.start();
}
}
/*
* 共享资源源实体类
*
*/
class Res2 {
public String userSex;
public String userName;
}
/*
*
* 输入线程资源
*
*/
class IntThrad2 extends Thread {
private Res2 res;
public IntThrad2(Res2 res) {
this.res = res;
}
@Override
public void run() {
int count = 0;
while (true) {
synchronized (res) {
if (count == 0) {
res.userName = "小明";
res.userSex = "男";
} else {
res.userName = "小红";
res.userSex = "女";
}
count = (count + 1) % 2;
}
}
}
}
/*
*
* 输出线程
*
*/
class OutThread2 extends Thread {
private Res2 res;
public OutThread2(Res2 res) {
this.res = res;
}
@Override
public void run() {
while (true) {
synchronized (res) {
System.out.println(res.userName + "--" + res.userSex);
}
}
}
}
class Res3 {
public String userSex;
public String userName;
// 线程通讯标识
public boolean flag = false;
}
class IntThrad3 extends Thread {
private Res3 res;
public IntThrad3(Res3 res) {
this.res = res;
}
@Override
public void run() {
int count = 0;
while (true) {
synchronized (res) {
if (res.flag) {
try {
// 当前线程变为等待,但是可以释放锁
res.wait();
} catch (Exception e) {
}
}
if (count == 0) {
res.userName = "小明";
res.userSex = "男";
} else {
res.userName = "小红";
res.userSex = "女";
}
count = (count + 1) % 2;
res.flag = true;
// 唤醒当前线程
res.notify();
}
}
}
}
class OutThread3 extends Thread {
private Res3 res;
public OutThread3(Res3 res) {
this.res = res;
}
@Override
public void run() {
while (true) {
synchronized (res) {
if (!res.flag) {
try {
res.wait();
} catch (Exception e) {
// TODO: handle exception
}
}
System.out.println(res.userName + "--" + res.userSex);
res.flag = false;
res.notify();
}
}
}
}
public class Test003 {
public static void main(String[] args) {
Res3 res = new Res3();
IntThrad3 intThrad = new IntThrad3(res);
OutThread3 outThread = new OutThread3(res);
intThrad.start();
outThread.start();
}
}
在 jdk1.5 之后,并发包中新增了 Lock 接口(以及相关实现类)用来实现锁功能,Lock 接口提供了与 synchronized 关键字类似的同步功能,但需要在使用时手动获取锁和释放锁。
Lock写法,基本用法
Lock lock = new ReentrantLock();
lock.lock();
try{
//可能会出现线程安全的操作
}finally{
//一定在finally中释放锁
//也不能把获取锁在try中进行,因为有可能在获取锁的时候抛出异常
lock.ublock();
}
Condition的功能类似于在传统的线程技术中的,Object.wait()和Object.notify()的功能。
代码示例,将上面将的例子改造如下
public class TL002_Condition {
public static void main(String[] args) throws InterruptedException {
Res res = new Res();
IntThread intThread = new IntThread(res);
OutThread outThread = new OutThread(res);
intThread.start();
outThread.start();
}
}
/*
* 共享资源
*/
class Res {
public String name;
public String sex;
public boolean flag = false;
public Lock lock = new ReentrantLock();
public Condition condition=lock.newCondition();
}
/*
* 写入线程
*/
class IntThread extends Thread {
public Res res;
public IntThread(Res res) {
this.res = res;
}
@Override
public void run() {
int count = 0; // 1
while (true) {
try {
res.lock.lock();
if(res.flag){
res.condition.await();
}
if (count == 0) {
res.name = "小明";
res.sex = "男";
} else {
res.name = "小红";
res.sex = "女";
}
count = (count + 1) % 2;
res.flag=true;
res.condition.signal();
} catch (Exception e) {
} finally {
res.lock.unlock();
}
}
}
}
// 读取线程
class OutThread extends Thread {
public Res res;
public OutThread(Res res) {
this.res = res;
}
@Override
public void run() {
while (true) {
try {
res.lock.lock();
if(!res.flag){
res.condition.await();
}
Thread.sleep(1000);
System.out.println(res.name + "," + res.sex);
res.flag = false;
res.condition.signal();
} catch (Exception e) {
// TODO: handle exception
} finally {
res.lock.unlock();
}
}
}
}
标签:boolean strong handle out 注意 机制 sleep 指定 读取
原文地址:https://www.cnblogs.com/haoworld/p/java-bing-fa-xian-cheng-tong-xin.html