为什么我的程序只执行了一次:
Thread-0放入:0
Thread-1取出:0
public class Test4{
public static void main(String[] args){
Querry q = new Querry();
new Producer(q).start();
new Customer(q).start();
}
}class Producer extends Thread{
Querry q;
public Producer(Querry q){
this.q = q;
}
public void run(){
try{
for(int i = 0; i< 10; i++){
if(q.isEmpty){
q.set(i);
notify();
}else
wait();
}
}catch (Exception e){
}
}}class Customer extends Thread{
Querry q;
public Customer(Querry q){
this.q = q;
} public void run(){
try{
for(int i = 0; i< 10; i++){
if(!q.isEmpty){
q.get();
notify();
}else
wait();
}
}catch (Exception e){
}
}
}class Querry {
int i = 0;
boolean isEmpty=true;
public synchronized void set(int i ){
this.i = i;
System.out.println(Thread.currentThread().getName() + "放入:" + i);
isEmpty = false;
}
public synchronized void get(){
System.out.println(Thread.currentThread().getName() + "取出:" + i);
isEmpty = true;
}
}
Thread-0放入:0
Thread-1取出:0
public class Test4{
public static void main(String[] args){
Querry q = new Querry();
new Producer(q).start();
new Customer(q).start();
}
}class Producer extends Thread{
Querry q;
public Producer(Querry q){
this.q = q;
}
public void run(){
try{
for(int i = 0; i< 10; i++){
if(q.isEmpty){
q.set(i);
notify();
}else
wait();
}
}catch (Exception e){
}
}}class Customer extends Thread{
Querry q;
public Customer(Querry q){
this.q = q;
} public void run(){
try{
for(int i = 0; i< 10; i++){
if(!q.isEmpty){
q.get();
notify();
}else
wait();
}
}catch (Exception e){
}
}
}class Querry {
int i = 0;
boolean isEmpty=true;
public synchronized void set(int i ){
this.i = i;
System.out.println(Thread.currentThread().getName() + "放入:" + i);
isEmpty = false;
}
public synchronized void get(){
System.out.println(Thread.currentThread().getName() + "取出:" + i);
isEmpty = true;
}
}
condition = true;
obj.notify();
}需要注意的概念是:# 调用obj的wait(), notify()方法前,必须获得obj锁,也就是必须写在synchronized(obj) {...} 代码段内。
# 调用obj.wait()后,线程A就释放了obj的锁,否则线程B无法获得obj锁,也就无法在synchronized(obj) {...} 代码段内唤醒A。
# 当obj.wait()方法返回后,线程A需要再次获得obj锁,才能继续执行。
# 如果A1,A2,A3都在obj.wait(),则B调用obj.notify()只能唤醒A1,A2,A3中的一个(具体哪一个由JVM决定)。
# obj.notifyAll()则能全部唤醒A1,A2,A3,但是要继续执行obj.wait()的下一条语句,必须获得obj锁,因此,A1,A2,A3只有一个有机会获得锁继续执行,例如A1,其余的需要等待A1释放obj锁之后才能继续执行。
# 当B调用obj.notify/notifyAll的时候,B正持有obj锁,因此,A1,A2,A3虽被唤醒,但是仍无法获得obj锁。直到B退出synchronized块,释放obj锁后,A1,A2,A3中的一个才有机会获得锁继续执行。 public class Test4 {
public static void main(String[] args) {
Querry q = new Querry();
new Producer(q).start();
new Customer(q).start(); }
}class Producer extends Thread { Querry q; public Producer(Querry q) {
this.q = q;
} public void run() {
try {
for (int i = 0; i < 10; i++) {
synchronized (q) {
if (q.isEmpty) {
q.set(i);
q.notify();
} else
q.wait();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}}class Customer extends Thread {
Querry q; public Customer(Querry q) {
this.q = q;
} public void run() {
try {
for (int i = 0; i < 10; i++) {
synchronized (q) {
if (!q.isEmpty) {
q.get();
q.notify();
} else
q.wait();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}class Querry {
int i = 0;
boolean isEmpty = true; public synchronized void set(int i) {
this.i = i;
System.out.println(Thread.currentThread().getName() + "放入:" + i);
isEmpty = false;
} public synchronized void get() {
System.out.println(Thread.currentThread().getName() + "取出:" + i);
isEmpty = true;
}}
public class Test4 {
public static void main(String[] args) {
Querry q = new Querry();
new Producer(q).start();
new Customer(q).start(); }
}class Producer extends Thread { Querry q; public Producer(Querry q) {
this.q = q;
} public void run() {
int i = 0;
try {
while (true) {
if (q.isEmpty) {
q.set(i);
notify();
} else
wait();
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}}class Customer extends Thread {
Querry q; public Customer(Querry q) {
this.q = q;
} public void run() {
try {
while (true) {
if (!q.isEmpty) {
q.get();
notify();
} else
wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}class Querry {
int i = 0;
boolean isEmpty = true; public synchronized void set(int i) {
this.i = i;
System.out.println(Thread.currentThread().getName() + "放入:" + i);
isEmpty = false;
} public synchronized void get() {
System.out.println(Thread.currentThread().getName() + "取出:" + i);
isEmpty = true;
}}
还是有问题,你再看看吧