java 10-04 死锁 notifyall()

------------------------------------------------------------------

同步代码

class TenProductCustorwait5{
public static void main(String[] agrs) {
Pool pool = new Pool();
Producer p = new Producer(pool);

p.setName("p1");
Consumer c = new Consumer(pool);

Consumer c1 = new Consumer(pool);

c .setName("c ");

c1 .setName("c1 ");


p.start();
c.start();

c1.start();



}
class Pool{
private int Max =1;
private java.util.List<Integer> list =new java.util.LinkedList<Integer>();

public void addLast(int i){
//以当前对象Pool做锁,把当前线程放到锁的队列中去

synchronized(this){

try{
while(list.size()>= Max){

Thread ct=Thread.currentThread();//获取当前线程的名称 .getName()

System.out.println(ct.getName()+".wait");
this.wait();
}

list.add(new Integer(i));

System.out.println(ct.getName()+":"+i);

System.out.println(ct.getName()+".notify");
this.notify();//通知和等待都是掉锁的方法

}

catch(Exception e){
e.printStackTrace();
}

}
}

public int Remove(){
//以当前对象Pool做锁,把当前线程放到锁的队列中去
synchronized(this){

try{
while(list.size()==0){

Thread ct=Thread.currentThread();//获取当前线程的名称 .getName()

System.out.println(ct.getName()+".wait");

this.wait();
}

int no =list.remove(0);

System.out.println(ct.getName()+":"+i);

System.out.println(ct.getName()+".notify");
this.notify();
return no;

}
catch(Exception e){

e.printStackTrace();
}
}

}
}

//在死锁的线程加上名称

class Producer extends Thread{

private Pool pool;
static int i =1;
public Producer( Pool pool){

this.name=name;
this.pool =pool;
}
public void run(){
while(true){
pool.addLast(i++);
System.out.println("生产者"+i);
}
}
}

class Consumer extends Thread{

private Pool pool;
public Consumer(Pool pool){
this.pool =pool;

this.name=name;
}
public void run(){
while(true){
int no = pool.Remove();
System.out.println("消费了"+no);
}
}
}

wait(int n)

-------------------------------------------------------------------------------------------------------------

class TenProductCustorwait5{
public static void main(String[] agrs) {
Pool pool = new Pool();
Producer p = new Producer(pool);

p.setName("p1");
Consumer c = new Consumer(pool);

Consumer c1 = new Consumer(pool);

c .setName("c ");

c1 .setName("c1 ");


p.start();
c.start();

c1.start();



}
class Pool{
private int Max =1;
private java.util.List<Integer> list =new java.util.LinkedList<Integer>();

public void addLast(int i){
//以当前对象Pool做锁,把当前线程放到锁的队列中去

synchronized(this){

try{
while(list.size()>= Max){

Thread ct=Thread.currentThread();//获取当前线程的名称 .getName()

System.out.println(ct.getName()+".wait");

//等待10毫秒 没人通知即撤 不等待了 或者用this.notifyAll();
this.wait(10);
}

list.add(new Integer(i));

System.out.println(ct.getName()+":"+i);

System.out.println(ct.getName()+".notify");
this.notify();//通知和等待都是掉锁的方法

//this.notifyAll();

}

catch(Exception e){
e.printStackTrace();
}

}
}

public int Remove(){
//以当前对象Pool做锁,把当前线程放到锁的队列中去
synchronized(this){

try{
while(list.size()==0){

Thread ct=Thread.currentThread();//获取当前线程的名称 .getName()

System.out.println(ct.getName()+".wait");

this.wait(10);
}

int no =list.remove(0);

System.out.println(ct.getName()+":"+i);

System.out.println(ct.getName()+".notify");
this.notify();

//this.notifyAll();
return no;

}
catch(Exception e){

e.printStackTrace();
}
}

}
}

//在死锁的线程加上名称

class Producer extends Thread{

private Pool pool;
static int i =1;
public Producer( Pool pool){

this.name=name;
this.pool =pool;
}
public void run(){
while(true){
pool.addLast(i++);
System.out.println("生产者"+i);
}
}
}

class Consumer extends Thread{

private Pool pool;
public Consumer(Pool pool){
this.pool =pool;

this.name=name;
}
public void run(){
while(true){
int no = pool.Remove();
System.out.println("消费了"+no);
}
}
}

原文地址:https://www.cnblogs.com/simly/p/10833232.html