我是Java编程的新手。我想使用Wait()和Notify()运行两个线程。但是我不能使用任务标志来进行线程同步,睡眠,屈服或等待(参数)。我写了它,但是我必须使用睡眠。有人可以帮助我在不睡觉的情况下将其更改为。这是我的主要班级
public class mainClass{
public static void main(String args[]) throws InterruptedException {
final Processor processor = new Processor();
for(int i=0; i<100; i++){
final int z = i;
Thread trainer = new Thread(new Runnable(){
public void run(){
try{
processor.produce(z);
}catch(InterruptedException e){
e.printStackTrace();
}
}
});
Thread sportsman = new Thread(new Runnable(){
public void run(){
try{
processor.consume(z);
}catch(InterruptedException e){
e.printStackTrace();
}
}
});
trainer.start();
sportsman.start();
trainer.join();
sportsman.join();
}
System.out.println("100 Tasks are Finished.");
}
}
这是我的第二堂课。
public class Processor {
public void produce(int n) throws InterruptedException {
synchronized (this){
System.out.println("Trainer making " + (n+1) + " Task..." );
wait();
System.out.println("");
}
}
public void consume(int m) throws InterruptedException {
Thread.sleep(1);
//I want to run the code without using sleep and get same output
synchronized (this){
System.out.println("Sportman doing " + (m+1) + " Task...");
notify();
}
}
}
这是我的输出。
Trainer making 1 Task...
Sportman doing 1 Task...
Trainer making 2 Task...
Sportman doing 2 Task...
.
.
.
Trainer making 99 Task...
Sportman doing 99 Task...
Trainer making 100 Task...
Sportman doing 100 Task...
100 Tasks are Finished.
谢谢。我的英语不好。对不起。
提示:
-
正确使用
wait
涉及等待特定的事情发生。正确的实现是这样的synchronize (x) { while (!x.itHasHappened()) { x.wait(); // for it to happen } }
循环是必要的,因为可以在原始锁上获得虚假的通知。
-
在您的特定示例中,问问自己必须等待什么。我认为你错了。
produce(N)
实际上在等什么,为什么?
在主流中,您创建了100次两个线程,我认为您应该仅创建两个线程,并且在这两个线程中运行了100次循环。
可能您需要做类似的事情...
- 生产者应共同创建100个任务(一次),然后等待每个任务,以完成消费者。
- 消费者应等待任务并在完成当前任务后通知生产者,他们等待下一个任务。
因此,您的主级应该看起来像这样,循环应该在生产者()和消费者()方法中。
public class mainClass {
public static void main(String args[]) throws InterruptedException {
final Processor processor = new Processor();
Thread trainer = new Thread(new Runnable() {
public void run() {
try {
processor.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread sportsman = new Thread(new Runnable() {
public void run() {
try {
processor.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
trainer.start();
sportsman.start();
trainer.join();
sportsman.join();
System.out.println("100 Tasks are Finished.");
}
}
和处理器也许是这样...
public class Processor {
private int taskNo = 0; // the number of the current task
// (0 = there is no task, but will be)
// (-1 = there won't be more task)
public void produce() throws InterruptedException {
synchronized (this) {
for (int i = 0; i < 100; i++) {
taskNo = i + 1; // making a task number (i+1)
System.out.println("Trainer making " + taskNo + " Task...");
notify(); // notifies the consumer that the task was made
wait(); // and waiting the consumer to finish... zzzz...
System.out.println("");
}
taskNo = -1; // there will be no more task
notify(); // notify the consumer about it
}
}
public void consume() throws InterruptedException {
synchronized (this) {
do {
if (taskNo == 0) {
wait(); // there is no task to do, waiting... zzzz...
}
if (taskNo != -1) {
System.out.println("Sportman doing " + taskNo + " Task...");
taskNo = 0; // sets the task to done
notify(); // notifies the producer that the task was done
}
} while (taskNo != -1);
}
}
}
通常有一个队列,而不是任务No变量,生产者将任务放置,并且消费者从IN中处理任务。但是,在您的情况下,队列一次只能有1个任务,因为生产者应等待消费者等待消费者完成。因此,您可以使用简单的变量(taskno)而不是队列。