wait-notify模型面试题
一道面试题:
启动两个线程, 一个输出 1,3,5,7…99, 另一个输出 2,4,6,8…100 最后 STDOUT 中按序输出 1,2,3,4,5…100
错误实现1:
public class NotifyErrorTest {
private int i = 1;
Thread t1 = new Thread(){
@Override
public void run() {
while (true) {
synchronized (this) {
notify();
if (i <= 100) {
System.out.println(currentThread().getName() + ":" + i);
i++;
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
};
Thread t2 = new Thread(){
@Override
public void run() {
while (true) {
synchronized (this) {
notify();
if (i <= 100) {
System.out.println(currentThread().getName() + ":" + i);
i++;
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
};
public static void main(String[] args){
NotifyErrorTest test = new NotifyErrorTest();
test.t1.start();
test.t2.start();
}
}
结果:
Thread-0:1
Thread-1:1
打印出这两个后,线程就一直被挂起了。为什么会这样呢。
先不考虑这种难看的重复代码需不需要重构,本身代码就有问题,虽然看起来都用了this,但是其实两个this所表示的含义不同,我们两个线程里面加上如下代码
System.out.println(this.getClass());
会发现打印出
class pers.marscheng.thread.NotifyErrorTest$1
class pers.marscheng.thread.NotifyErrorTest$2
原来两个this不是同一个对象,匿名类会生成新的对象,所以导致两个线程获取的monitor锁是不同的。这就导致wait()方法调用之后,两个线程都被挂起,但是再也没人能把他们唤醒,而且由于锁不同,两个线程都同时执行了,打印出的都是1。
正确实现:
public class NotifyTest implements Runnable {
int i = 1;
public static void main(String[] args) {
NotifyTest test = new NotifyTest();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.start();
t2.start();
}
@Override
public void run() {
while (true) {
synchronized (this) {
this.notify();
if (i <= 100) {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + ":" + i);
i++;
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
通过condition实现:
public class ConditionTest implements Runnable{
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
int i = 1;
@Override
public void run() {
try {
lock.lock();
while (true) {
condition.signal();
if (i <= 100) {
System.out.println(Thread.currentThread().getName() + ":" + i);
i++;
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
ConditionTest test = new ConditionTest();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.start();
t2.start();
}
}
拓展:
启动三个线程, 一个输出 1,4,7,10…100, 一个输出 2,5,8,11…101,最后一个暑促3,6,9,12...102 最后 STDOUT 中按序输出 1,2,3,4,5…102
实现:
public class NotifyTest2 implements Runnable {
private Object prev;
private Object self;
AtomicInteger i;
private NotifyTest2(AtomicInteger num,Object prev, Object self) {
this.i = num;
this.prev = prev;
this.self = self;
}
@Override
public void run() {
while (true) {
synchronized (prev) {
synchronized (self) {
if (i.get() <= 102) {
System.out.println(Thread.currentThread().getName() + ":" + i.get());
i.getAndIncrement();
self.notify();
}
}
try {
prev.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Object a = new Object();
Object b = new Object();
Object c = new Object();
AtomicInteger num = new AtomicInteger(1);
NotifyTest2 testA = new NotifyTest2(num,c,a);
NotifyTest2 testB = new NotifyTest2(num,a,b);
NotifyTest2 testC = new NotifyTest2(num,b,c);
new Thread(testA).start();
new Thread(testB).start();
new Thread(testC).start();
}
}
利用AtomicInteger做为共享变量。
wait-notify模型面试题的更多相关文章
- JVM内存模型和面试题解析
一.JVM运行时区域 其中, 线程私有的:程序计数器,虚拟机栈,本地方法栈 线程共享的:堆,方法区,直接内存 1 程序计数器 程序计数器是一块较小的内存空间,可以看作是当前线程所执行的字节码的行号指示 ...
- 不止面试02-JVM内存模型面试题详解
第一部分:面试题 本篇文章我们将尝试回答以下问题: 描述一下jvm的内存结构 描述一下jvm的内存模型 谈一下你对常量池的理解 什么情况下会发生栈内存溢出?和内存溢出有什么不同? String str ...
- Java多线程与并发基础
CS-LogN思维导图:记录专业基础 面试题 开源地址:https://github.com/FISHers6/CS-LogN 多线程与并发基础 实现多线程 面试题1:有几种实现线程的方法,分别是什么 ...
- CCS+C6678LE开发记录11:多核协作(IPC)入门
为更好地发挥C6678的多核性能,需要用到多核协作.幸运的是,我们可以使用官方提供的IPC模块. IPC=Inter-Processor Communication, 核间通信,粗略来说就是多核之间进 ...
- 详解CurrentHashMap之预习篇
CurrentHashMap的出现时为了解决HashMap的高并发导致OOM的缺陷,并且能够保证高性能读取.那么解读CurrentHashMap需要具备哪些知识的呢? HashMap 解读 Java ...
- 关于Java高并发编程你需要知道的“升段攻略”
关于Java高并发编程你需要知道的"升段攻略" 基础 Thread对象调用start()方法包含的步骤 通过jvm告诉操作系统创建Thread 操作系统开辟内存并使用Windows ...
- Java基础(补充)
为什么 Java 中只有值传递? 开始之前,我们先来搞懂下面这两个概念: 形参&实参 值传递&引用传递 形参&实参 方法的定义可能会用到 参数(有参的方法),参数在程序语言中分 ...
- 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...
- 【java线程系列】java线程系列之线程间的交互wait()/notify()/notifyAll()及生产者与消费者模型
关于线程,博主写过java线程详解基本上把java线程的基础知识都讲解到位了,但是那还远远不够,多线程的存在就是为了让多个线程去协作来完成某一具体任务,比如生产者与消费者模型,因此了解线程间的协作是非 ...
随机推荐
- sql 全站搜索
SQL全站搜索 create proc Full_Search(@string varchar(50)) as begin declare @tbname varchar(50) declare tb ...
- 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or 'Inorder and ...
- C# linq学习【转】
在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...
- javascript特殊值常量
Infinity 表示无穷大的特殊值.当数字运算结果超出javascript能表示的数字范围时. Nan 特殊的非数字值(not a number).0除0.数字运算符的操作数为字符等情况. Numb ...
- CF831C Jury Marks
思路: 关键在于“插入”一个得分之后,其他所有得分也随之确定了. 实现: #include <iostream> #include <cstdio> #include < ...
- [Codeforces]Codeforces Round #490 (Div. 3)
Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...
- 关于编辑器对<input>标签报错提示“表单输入没有相关label”的问题
相信很多朋友在制作表单的时候,我们的编辑器会有下图的相关提示吧 我们发现虽然这样并不影响我们的正常使用,但是看着这样的报错提示总是很让人心烦,那么这到底是为什么呢? 其实,这是因为编辑器建议我们在使用 ...
- ie9以下的浏览器兼容性问题
.bind不兼容的问题Function.prototype.bind = function () { var fn = this, args = Array.prototype.slice.call( ...
- Product Device Lot
Product是指产品: 这个Product可以在不同的设备类型上生产, 同一类型的设备也可能硬件有差异,所以会有相对应的Device(Recipe): 同一Product(或同一Device)由于数 ...
- 《Java编程的逻辑》第二部分 面向对象