当interrupt方法遇到wait方法

当线程呈wait()状态时,调用线程对象的interrupt()会出现InterruptedException异常

package Third;

public class Service {

    public void testMethod(Object lock) {
try {
synchronized (lock) {
System.out.println("begin wait()");
lock.wait();
System.out.println(" end wait()");
}
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("出现异常了,因为呈wait状态的线程被interrupt了!");
}
} }
package Third;

public class ThreadA extends Thread {

    private Object lock;

    public ThreadA(Object lock) {
super();
this.lock = lock;
} @Override
public void run() {
Service service = new Service();
service.testMethod(lock);
} }
package Third;

public class Test {

    public static void main(String[] args) {

        try {
Object lock = new Object(); ThreadA a = new ThreadA(lock);
a.start(); Thread.sleep(5000); a.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
} } }

只通知一个线程

调用方法notify()一次只随机通知一个线程进行唤醒

package Third;

public class Service {

    public void testMethod(Object lock) {
try {
synchronized (lock) {
System.out.println("begin wait() ThreadName="
+ Thread.currentThread().getName());
lock.wait();
System.out.println(" end wait() ThreadName="
+ Thread.currentThread().getName());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
package Third;

public class ThreadA extends Thread {
private Object lock; public ThreadA(Object lock) {
super();
this.lock = lock;
} @Override
public void run() {
Service service = new Service();
service.testMethod(lock);
} }
package Third;

public class ThreadB extends Thread {
private Object lock; public ThreadB(Object lock) {
super();
this.lock = lock;
} @Override
public void run() {
Service service = new Service();
service.testMethod(lock);
} }
package Third;

public class ThreadC extends Thread {
private Object lock; public ThreadC(Object lock) {
super();
this.lock = lock;
} @Override
public void run() {
Service service = new Service();
service.testMethod(lock);
} }
package Third;

public class NotifyThread extends Thread {
private Object lock; public NotifyThread(Object lock) {
super();
this.lock = lock;
} @Override
public void run() {
synchronized (lock) {
lock.notify(); }
} }
package Third;

public class Test {

    public static void main(String[] args) throws InterruptedException {

        Object lock = new Object();

        ThreadA a = new ThreadA(lock);
a.start(); ThreadB b = new ThreadB(lock);
b.start(); ThreadC c = new ThreadC(lock);
c.start(); Thread.sleep(1000); NotifyThread notifyThread = new NotifyThread(lock);
notifyThread.start(); } }

唤醒所有线程

package Third;

public class NotifyThread extends Thread {
private Object lock; public NotifyThread(Object lock) {
super();
this.lock = lock;
} @Override
public void run() {
synchronized (lock) {
lock.notifyAll(); }
} }

wait(long)的使用

功能是等待某一时间内是否有线程对锁进行唤醒,如果超过这个时间则自动唤醒

当然也可以由其他线程进行唤醒

package Third;

public class MyRunnable {
static private Object lock = new Object();
static private Runnable runnable1 = new Runnable() {
@Override
public void run() {
try {
synchronized (lock) {
System.out.println("wait begin timer="
+ System.currentTimeMillis());
lock.wait(5000);
System.out.println("wait end timer="
+ System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}; static private Runnable runnable2 = new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("notify begin timer="
+ System.currentTimeMillis());
lock.notify();
System.out.println("notify end timer="
+ System.currentTimeMillis());
}
}
}; public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(runnable1);
t1.start();
Thread.sleep(3000);
Thread t2 = new Thread(runnable2);
t2.start();
} }

通知过早

package Third;

public class MyRun {

    private String lock = new String("");

    private Runnable runnableA = new Runnable() {
@Override
public void run() {
try {
synchronized (lock) { System.out.println("begin wait");
lock.wait();
System.out.println("end wait"); }
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}; private Runnable runnableB = new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("begin notify");
lock.notify();
System.out.println("end notify"); }
}
}; public static void main(String[] args) throws InterruptedException { MyRun run = new MyRun(); Thread a = new Thread(run.runnableA);
a.start(); Thread b = new Thread(run.runnableB);
b.start(); } }

package Third;

public class MyRun {

    private String lock = new String("");

    private Runnable runnableA = new Runnable() {
@Override
public void run() {
try {
synchronized (lock) { System.out.println("begin wait");
lock.wait();
System.out.println("end wait"); }
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}; private Runnable runnableB = new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("begin notify");
lock.notify();
System.out.println("end notify"); }
}
}; public static void main(String[] args) throws InterruptedException { MyRun run = new MyRun(); Thread a = new Thread(run.runnableA);
a.start(); Thread.sleep(100); Thread b = new Thread(run.runnableB);
b.start(); } }

如果先通知了,则wait方法也就没有必要执行了

package Third;

public class MyRun {

    private String lock = new String("");
private boolean isFirstRunB = false; private Runnable runnableA = new Runnable() {
@Override
public void run() {
try {
synchronized (lock) {
while (isFirstRunB == false) {
System.out.println("begin wait");
lock.wait();
System.out.println("end wait");
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}; private Runnable runnableB = new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("begin notify");
lock.notify();
System.out.println("end notify");
isFirstRunB = true; }
}
}; public static void main(String[] args) throws InterruptedException { MyRun run = new MyRun();
Thread b = new Thread(run.runnableB);
b.start();
Thread.sleep(100);
Thread a = new Thread(run.runnableA);
a.start(); } }

等待wait条件发生变化

在使用wait/notify模式时,还需要注意另外一种情况。也就是wait等待的条件发生了变化,也容易造成程序逻辑混乱

package Third;

public class Add {

    private String lock;

    public Add(String lock) {
super();
this.lock = lock;
} public void add() {
synchronized (lock) {
ValueObject.list.add("anyString");
lock.notifyAll();
}
} }
package Third;

public class Subtract {

    private String lock;

    public Subtract(String lock) {
super();
this.lock = lock;
} public void subtract() {
try {
synchronized (lock) {
if (ValueObject.list.size() == 0) {
System.out.println("wait begin ThreadName="
+ Thread.currentThread().getName());
lock.wait();
System.out.println("wait end ThreadName="
+ Thread.currentThread().getName());
}
ValueObject.list.remove(0);
System.out.println("list size=" + ValueObject.list.size());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
package Third;

import java.util.ArrayList;
import java.util.List; public class ValueObject { public static List list = new ArrayList(); }
package Third;

public class ThreadAdd extends Thread {

    private Add p;

    public ThreadAdd(Add p) {
super();
this.p = p;
} @Override
public void run() {
p.add();
} }
package Third;

public class ThreadSubtract extends Thread {

    private Subtract r;

    public ThreadSubtract(Subtract r) {
super();
this.r = r;
} @Override
public void run() {
r.subtract();
} }
package Third;

public class Run {

    public static void main(String[] args) throws InterruptedException {

        String lock = new String("");

        Add add = new Add(lock);
Subtract subtract = new Subtract(lock); ThreadSubtract subtract1Thread = new ThreadSubtract(subtract);
subtract1Thread.setName("subtract1Thread");
subtract1Thread.start(); ThreadSubtract subtract2Thread = new ThreadSubtract(subtract);
subtract2Thread.setName("subtract2Thread");
subtract2Thread.start(); Thread.sleep(1000); ThreadAdd addThread = new ThreadAdd(add);
addThread.setName("addThread");
addThread.start(); } }

package Third;

public class Subtract {

    private String lock;

    public Subtract(String lock) {
super();
this.lock = lock;
} public void subtract() {
try {
synchronized (lock) {
while (ValueObject.list.size() == 0) {
System.out.println("wait begin ThreadName="
+ Thread.currentThread().getName());
lock.wait();
System.out.println("wait end ThreadName="
+ Thread.currentThread().getName());
}
ValueObject.list.remove(0);
System.out.println("list size=" + ValueObject.list.size());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} }

生产者/消费者模式实现

1、一生产与一消费:操作值

package Third;

//生产者
public class P { private String lock; public P(String lock) {
super();
this.lock = lock;
} public void setValue() {
try {
synchronized (lock) {
if (!ValueObject.value.equals("")) {
lock.wait();
}
String value = System.currentTimeMillis() + "_"
+ System.nanoTime();
System.out.println("set的值是" + value);
ValueObject.value = value;
lock.notify();
} } catch (InterruptedException e) {
e.printStackTrace();
}
} }
package Third;

//消费者
public class C { private String lock; public C(String lock) {
super();
this.lock = lock;
} public void getValue() {
try {
synchronized (lock) {
if (ValueObject.value.equals("")) {
lock.wait();
}
System.out.println("get的值是" + ValueObject.value);
ValueObject.value = "";
lock.notify();
} } catch (InterruptedException e) {
e.printStackTrace();
}
} }
package Third;

public class ValueObject {

    public static String value = "";

}
package Third;

public class ThreadP extends Thread {

    private P p;

    public ThreadP(P p) {
super();
this.p = p;
} @Override
public void run() {
while (true) {
p.setValue();
}
} }
package Third;

public class ThreadC extends Thread {

    private C r;

    public ThreadC(C r) {
super();
this.r = r;
} @Override
public void run() {
while (true) {
r.getValue();
}
} }
package Third;

public class Run {

    public static void main(String[] args) {

        String lock = new String("");
P p = new P(lock);
C r = new C(lock); ThreadP pThread = new ThreadP(p);
ThreadC rThread = new ThreadC(r); pThread.start();
rThread.start();
} }

在控制台中打印的日志get和set是交替运行的

2、多生产与多消费:操作值-假死

“”假死“”的现象其实就是线程进入waiting等待状态,如果全部线程都进入waiting状态,则程序就不再执行任何业务功能了,整个项目呈停止状态

package Third;

//消费者
public class C { private String lock; public C(String lock) {
super();
this.lock = lock;
} public void getValue() {
try {
synchronized (lock) {
while (ValueObject.value.equals("")) {
System.out.println("消费者 "
+ Thread.currentThread().getName() + " WAITING了☆");
lock.wait();
}
System.out.println("消费者 " + Thread.currentThread().getName()
+ " RUNNABLE了");
ValueObject.value = "";
lock.notify();
} } catch (InterruptedException e) {
e.printStackTrace();
}
} }
package Third;

//生产者
public class P { private String lock; public P(String lock) {
super();
this.lock = lock;
} public void setValue() {
try {
synchronized (lock) {
while (!ValueObject.value.equals("")) {
System.out.println("生产者 "
+ Thread.currentThread().getName() + " WAITING了★");
lock.wait();
}
System.out.println("生产者 " + Thread.currentThread().getName()
+ " RUNNABLE了");
String value = System.currentTimeMillis() + "_"
+ System.nanoTime();
ValueObject.value = value;
lock.notify();
} } catch (InterruptedException e) {
e.printStackTrace();
}
} }
package Third;

public class ValueObject {

    public static String value = "";

}
package Third;

public class ThreadP extends Thread {

    private P p;

    public ThreadP(P p) {
super();
this.p = p;
} @Override
public void run() {
while (true) {
p.setValue();
}
} }
package Third;

public class ThreadC extends Thread {

    private C r;

    public ThreadC(C r) {
super();
this.r = r;
} @Override
public void run() {
while (true) {
r.getValue();
}
} }
package Third;

public class Run {

    public static void main(String[] args) throws InterruptedException {

        String lock = new String("");
P p = new P(lock);
C r = new C(lock); ThreadP[] pThread = new ThreadP[2];
ThreadC[] rThread = new ThreadC[2]; for (int i = 0; i < 2; i++) {
pThread[i] = new ThreadP(p);
pThread[i].setName("生产者" + (i + 1)); rThread[i] = new ThreadC(r);
rThread[i].setName("消费者" + (i + 1)); pThread[i].start();
rThread[i].start();
} Thread.sleep(5000);
Thread[] threadArray = new Thread[Thread.currentThread()
.getThreadGroup().activeCount()];
Thread.currentThread().getThreadGroup().enumerate(threadArray); for (int i = 0; i < threadArray.length; i++) {
System.out.println(threadArray[i].getName() + " "
+ threadArray[i].getState());
}
} }

假死出现的主要原因是有可能连续唤醒同类

多生产与多消费:操作值

将上面项目的P.java和C.java文件中的notify()改成notifyAll()即可,不光通知同类线程,也包括异类

《Java多线程编程核心技术》读后感(九)的更多相关文章

  1. Java多线程编程核心技术---学习分享

    继承Thread类实现多线程 public class MyThread extends Thread { @Override public void run() { super.run(); Sys ...

  2. Java多线程编程核心技术---对象及变量的并发访问(二)

    数据类型String的常量池特性 在JVM中具有String常量池缓存的功能. public class Service { public static void print(String str){ ...

  3. Java多线程编程核心技术

    Java多线程编程核心技术 这本书有利于对Java多线程API的理解,但不容易从中总结规律. JDK文档 1. Thread类 部分源码: public class Thread implements ...

  4. 《Java多线程编程核心技术》推荐

    写这篇博客主要是给猿友们推荐一本书<Java多线程编程核心技术>. 之所以要推荐它,主要因为这本书写得十分通俗易懂,以实例贯穿整本书,使得原本抽象的概念,理解起来不再抽象. 只要你有一点点 ...

  5. 《java多线程编程核心技术》(一)使用多线程

    了解多线程 进程和多线程的概念和线程的优点: 提及多线程技术,不得不提及"进程"这个概念.百度百科对"进程"的解释如下: 进程(Process)是计算机中的程序 ...

  6. 《Java 多线程编程核心技术》- 笔记

    作为业务开发人员,能够在工作中用到的技术其实不多.虽然平时老是说什么,多线程,并发,注入,攻击!但是在实际工作中,这些东西不见得用得上.因为,我们用的框架已经把这些事做掉了. 比如web开发,外面有大 ...

  7. Thread.currentThread()和this的区别——《Java多线程编程核心技术》

    前言:在阅读<Java多线程编程核心技术>过程中,对书中程序代码Thread.currentThread()与this的区别有点混淆,这里记录下来,加深印象与理解. 具体代码如下: pub ...

  8. Java多线程编程核心技术(三)多线程通信

    线程是操作系统中独立的个体,但这些个体如果不经过特殊的处理就不能成为一个整体.线程间的通信就是成为整体的必用方案之一,可以说,使线程间进行通信后,系统之间的交互性会更强大,在大大提高CPU利用率的同时 ...

  9. Java多线程编程核心技术(二)对象及变量的并发访问

    本文主要介绍Java多线程中的同步,也就是如何在Java语言中写出线程安全的程序,如何在Java语言中解决非线程安全的相关问题.阅读本文应该着重掌握如下技术点: synchronized对象监视器为O ...

  10. Java多线程编程核心技术(一)Java多线程技能

    1.进程和线程 一个程序就是一个进程,而一个程序中的多个任务则被称为线程. 进程是表示资源分配的基本单位,线程是进程中执行运算的最小单位,亦是调度运行的基本单位. 举个例子: 打开你的计算机上的任务管 ...

随机推荐

  1. Linux系统中的python2.x升级至python3

    python2.x升级至python3 由于需要在公司测试环境搭建的Jenkins上面运行接口自动化脚本,所以需要对Jenkins所处的服务器的python环境进行升级,以便运行python3编写的脚 ...

  2. ES5中的类与继承

    最近在重新复习TypeScript,看到类这块的时候自然会和ES5中的类写法进行对比加深印象. 发现ES5的类与继承一些细节还是挺多的,时间久了容易忘记,特此记录下. 首先是ES5的类定义,这没什么好 ...

  3. 基于ajax的登录

    验证码 当登录一个网站的时候往往会有验证码. python生成随机验证码,需要使用到 PIL 模块 安装 : pip3 install pillow 1. 创建图片        我们现在写的验证码属 ...

  4. debian下编译openwrt固件

    参考文章:Ubuntu下编译OpenWRT固件 我买的路由器是RG100A-AA,采用了bcm63xx系列的芯片. 下载openwrt源码: svn co svn://svn.openwrt.org/ ...

  5. C# HttpRequest

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  6. HDU 1201 Fibonacci Again

    Fibonacci Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. mysql too many connections 解决方法

    1.mysql -u root -p 回车输入密码进入mysql   2.show processlist; 查看连接数,可以发现有很多连接处于sleep状态,这些其实是暂时没有用的,所以可以kill ...

  8. Starting MySQL.. ERROR! The server quit without updating PID file

    版权声明:本文为博主原创文章,未经博主允许不得转载. 注意:本文出自 “阿飞”的博客 ,如果要转载本文章,请与作者联系! 并注明来源: http://blog.csdn.net/faye0412/ar ...

  9. 创建blog APP

    声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 什么是APP呢,Django里的APP其 ...

  10. Java常用四大线程池用法以及ThreadPoolExecutor详解

    为什么用线程池? 1.创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处-理效率 2.线程并发数量过多,抢占系统资源从而导致阻塞 3.对线程进行一些简单的管理 在Java中,线 ...