代码有什么问题?为什么线程不交错?
Whats wrong with the code? Why are threads not interleaving?
class Producer implements Runnable{ //producer job here
Vector<Integer> sQueue;
public Producer(Vector<Integer> queue) {
this.sQueue=queue;
}
public void run(){
int i=0;
try{
synchronized(sQueue){
for(i=0;i<30;i++){
if(sQueue.size()<15){
System.out.println("Producer producing the item:-"+i);
sQueue.add(i);
sQueue.notifyAll();
}
else
{ sQueue.notifyAll();
sQueue.wait();
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
class Consumer implements Runnable{
Vector<Integer> sQueue;
public Consumer(Vector<Integer> queue) {
this.sQueue=queue;
}
public void run(){
int i=0;
try{
synchronized(sQueue){
while(true){
if(sQueue.size()>0){
System.out.println("Consumer is removing the item:-");
int item=sQueue.remove(0);
System.out.println(item);
sQueue.notifyAll();
Thread.sleep(1000);
}
else
{
sQueue.wait();
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
public class ProducerConsumerProblem {
public static void main(String [] arg){
Vector<Integer> sQueue;
sQueue = new Vector<>(15);
Producer p=new Producer(sQueue);
Consumer c=new Consumer(sQueue);
Thread producer=new Thread(p);
Thread consumer=new Thread(c);
producer.start();
consumer.start();
}
}
在上面的代码中,我试图在 java 多线程的帮助下模拟生产者消费者问题。我正在使用 sQueue 作为整数共享队列我不知道出了什么问题,生产者线程和消费者线程没有交错,这是我机器上的输出
----------------------------output------------------------
Producer producing the item:-0
Producer producing the item:-1
Producer producing the item:-2
Producer producing the item:-3
Producer producing the item:-4
Producer producing the item:-5
Producer producing the item:-6
Producer producing the item:-7
Producer producing the item:-8
Producer producing the item:-9
Producer producing the item:-10
Producer producing the item:-11
Producer producing the item:-12
Producer producing the item:-13
Producer producing the item:-14
Consumer is removing the item:-
0
Consumer is removing the item:-
1
Consumer is removing the item:-
2
Consumer is removing the item:-
3
Consumer is removing the item:-
4
Consumer is removing the item:-
5
Consumer is removing the item:-
6
Consumer is removing the item:-
7
Consumer is removing the item:-
8
Consumer is removing the item:-
9
Consumer is removing the item:-
10
Consumer is removing the item:-
11
Consumer is removing the item:-
12
Consumer is removing the item:-
13
Consumer is removing the item:-
14
Producer producing the item:-16
Producer producing the item:-17
Producer producing the item:-18
Producer producing the item:-19
Producer producing the item:-20
Producer producing the item:-21
Producer producing the item:-22
Producer producing the item:-23
Producer producing the item:-24
Producer producing the item:-25
Producer producing the item:-26
Producer producing the item:-27
Producer producing the item:-28
Producer producing the item:-29
Consumer is removing the item:-
16
Consumer is removing the item:-
17
Consumer is removing the item:-
18
Consumer is removing the item:-
19
Consumer is removing the item:-
20
Consumer is removing the item:-
21
Consumer is removing the item:-
22
Consumer is removing the item:-
23
Consumer is removing the item:-
24
Consumer is removing the item:-
25
Consumer is removing the item:-
26
Consumer is removing the item:-
27
Consumer is removing the item:-
28
Consumer is removing the item:-
29
你的方法是synchronized
。当 Producer
开始时,它获得队列上的锁,并且在完成之前不会释放它。当您的 Consumer
启动时,它会一直等待直到获得锁。如果您希望它们交错,请不要同步... synchronized
关键字专门用于 防止 交错。
class Producer implements Runnable{ //producer job here
Vector<Integer> sQueue;
public Producer(Vector<Integer> queue) {
this.sQueue=queue;
}
public void run(){
int i=0;
try{
synchronized(sQueue){
for(i=0;i<30;i++){
if(sQueue.size()<15){
System.out.println("Producer producing the item:-"+i);
sQueue.add(i);
sQueue.notifyAll();
}
else
{ sQueue.notifyAll();
sQueue.wait();
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
class Consumer implements Runnable{
Vector<Integer> sQueue;
public Consumer(Vector<Integer> queue) {
this.sQueue=queue;
}
public void run(){
int i=0;
try{
synchronized(sQueue){
while(true){
if(sQueue.size()>0){
System.out.println("Consumer is removing the item:-");
int item=sQueue.remove(0);
System.out.println(item);
sQueue.notifyAll();
Thread.sleep(1000);
}
else
{
sQueue.wait();
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
public class ProducerConsumerProblem {
public static void main(String [] arg){
Vector<Integer> sQueue;
sQueue = new Vector<>(15);
Producer p=new Producer(sQueue);
Consumer c=new Consumer(sQueue);
Thread producer=new Thread(p);
Thread consumer=new Thread(c);
producer.start();
consumer.start();
}
}
在上面的代码中,我试图在 java 多线程的帮助下模拟生产者消费者问题。我正在使用 sQueue 作为整数共享队列我不知道出了什么问题,生产者线程和消费者线程没有交错,这是我机器上的输出
----------------------------output------------------------
Producer producing the item:-0
Producer producing the item:-1
Producer producing the item:-2
Producer producing the item:-3
Producer producing the item:-4
Producer producing the item:-5
Producer producing the item:-6
Producer producing the item:-7
Producer producing the item:-8
Producer producing the item:-9
Producer producing the item:-10
Producer producing the item:-11
Producer producing the item:-12
Producer producing the item:-13
Producer producing the item:-14
Consumer is removing the item:-
0
Consumer is removing the item:-
1
Consumer is removing the item:-
2
Consumer is removing the item:-
3
Consumer is removing the item:-
4
Consumer is removing the item:-
5
Consumer is removing the item:-
6
Consumer is removing the item:-
7
Consumer is removing the item:-
8
Consumer is removing the item:-
9
Consumer is removing the item:-
10
Consumer is removing the item:-
11
Consumer is removing the item:-
12
Consumer is removing the item:-
13
Consumer is removing the item:-
14
Producer producing the item:-16
Producer producing the item:-17
Producer producing the item:-18
Producer producing the item:-19
Producer producing the item:-20
Producer producing the item:-21
Producer producing the item:-22
Producer producing the item:-23
Producer producing the item:-24
Producer producing the item:-25
Producer producing the item:-26
Producer producing the item:-27
Producer producing the item:-28
Producer producing the item:-29
Consumer is removing the item:-
16
Consumer is removing the item:-
17
Consumer is removing the item:-
18
Consumer is removing the item:-
19
Consumer is removing the item:-
20
Consumer is removing the item:-
21
Consumer is removing the item:-
22
Consumer is removing the item:-
23
Consumer is removing the item:-
24
Consumer is removing the item:-
25
Consumer is removing the item:-
26
Consumer is removing the item:-
27
Consumer is removing the item:-
28
Consumer is removing the item:-
29
你的方法是synchronized
。当 Producer
开始时,它获得队列上的锁,并且在完成之前不会释放它。当您的 Consumer
启动时,它会一直等待直到获得锁。如果您希望它们交错,请不要同步... synchronized
关键字专门用于 防止 交错。