public class MyDeadLockTest {
public static void main(String[] args){
Object obj1 = new Object();
Thread thread1 = new Thread(new DeadRes(true,obj1));
Thread thread2 = new Thread(new DeadRes(false,obj1));
thread1.start();
thread2.start();
}
}
class DeadRes implements Runnable{
boolean flag;
Object obj;
public DeadRes(boolean flag, Object obj1) {
this.flag = flag;
this.obj = obj1;
}
@Override
public void run() {
if(flag){
synchronized (DeadRes.class){
System.out.println(Thread.currentThread().getName()+" acquie lock is DeadRes.class");
synchronized (obj){
System.out.println(Thread.currentThread().getName()+" acquie lock is obj");
}
}
}else{
synchronized (obj){
System.out.println(Thread.currentThread().getName()+" acquie lock is obj");
synchronized (DeadRes.class){
System.out.println(Thread.currentThread().getName()+" acquie lock is DeadRes.class");
}
}
}
}
}
Thread-1 acquie lock is obj Thread-0 acquie lock is DeadRes.class
public class ThreadProConsume {
public static void main(String[] args){
Product product = new Product();
Thread thread1 = new Thread(new Producer(product));
Thread thread2 = new Thread(new Consumer(product));
thread1.start();
thread2.start();
}
}
class Product{
String name;
private int count = 1;
boolean flag = false;
public synchronized void set(String name){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name +"--"+count++;
flag = true;
System.out.println(Thread.currentThread().getName()+" produce num : "+this.name);
this.notify();
}
public synchronized void out(){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" consume num is : "+this.name);
flag = false;
this.notify();
}
}
class Producer implements Runnable{
Product res;
public Producer(Product product) {
this.res = product;
}
@Override
public void run() {
while(true){
res.set("guyue");
}
}
}
class Consumer implements Runnable{
Product res;
public Consumer(Product product) {
this.res = product;
}
@Override
public void run() {
while(true){
res.out();
}
}
}
Thread-1 consume num is : guyue--3938 Thread-0 produce num : guyue--3939 Thread-1 consume num is : guyue--3939 Thread-0 produce num : guyue--3940 Thread-1 consume num is : guyue--3940 Thread-0 produce num : guyue--3941 Thread-1 consume num is : guyue--3941
public class ThreadProConsume {
public static void main(String[] args){
Product product = new Product();
Thread thread1 = new Thread(new Producer(product));
Thread thread3 = new Thread(new Producer(product));
Thread thread2 = new Thread(new Consumer(product));
Thread thread4 = new Thread(new Consumer(product));
thread1.start();
thread3.start();
thread2.start();
thread4.start();
}
}
class Product{
String name;
private int count = 1;
boolean flag = false;
public synchronized void set(String name){
while(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name +"--"+count++;
flag = true;
System.out.println(Thread.currentThread().getName()+" produce num : "+this.name);
this.notifyAll();
}
public synchronized void out(){
while (!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" consume num is : "+this.name);
flag = false;
this.notifyAll();
}
}
Thread-0 produce num : guyue--50325 Thread-2 consume num is : guyue--50325 Thread-1 produce num : guyue--50326 Thread-3 consume num is : guyue--50326 Thread-0 produce num : guyue--50327 Thread-2 consume num is : guyue--50327 Thread-1 produce num : guyue--50328 Thread-3 consume num is : guyue--50328
class Product{
String name;
private int count = 1;
boolean flag = false;
Lock lock = new ReentrantLock();
Condition conditon = lock.newCondition();
public void set(String name){
try{
lock.lock();
while(flag){
conditon.await();
}
this.name = name +"--"+count++;
flag = true;
System.out.println(Thread.currentThread().getName()+" produce num : "+this.name);
conditon.signalAll();
}catch (Exception e){
}finally {
lock.unlock();
}
}
public void out(){
try{
lock.lock();
while(!flag){
conditon.await();
}
flag = false;
System.out.println(Thread.currentThread().getName()+" consumer num is : "+this.name);
conditon.signalAll();
}catch (Exception e){
}finally {
lock.unlock();
}
}
}
Thread-0 produce num : guyue--20305 Thread-3 consumer num is : guyue--20305 Thread-1 produce num : guyue--20306 Thread-2 consumer num is : guyue--20306 Thread-0 produce num : guyue--20307 Thread-3 consumer num is : guyue--20307 Thread-1 produce num : guyue--20308 Thread-2 consumer num is : guyue--20308
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有