gpt4 book ai didi

java - 如何让两个线程等待并互相通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:38 24 4
gpt4 key购买 nike

我必须创建两个线程,它们必须以 2 秒的间隔从队列中轮询和反对。

第一个线程轮询和对象然后等待并通知第二个线程从其队列中轮询对象。

我阅读了所有关于等待和通知的内容,但对我没有任何作用。

有什么建议吗?

第一个线程:

public class SouthThread extends Thread {

private Queue<Car> q = new LinkedList<Car>();

public void CreateQueue() {

Scanner input = new Scanner(System.in);

for (int i = 0; i < 2; i++) {
Car c = new Car();
System.out.println("Enter registration number: ");
String regNum = input.nextLine();
c.setRegNum(regNum);
q.offer(c);
}
}

public int getQueueSize() {
return q.size();
}

@Override
public void run() {
while (q.size() != 0)
try {
while (q.size() != 0) {
synchronized (this) {
System.out.print("The car with registration number: ");
System.out.print(q.poll().getRegNum());
System.out
.println(" have passed the bridge from the south side.");
this.wait(2000);
notify();

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

第二个线程:

public class NorthThread extends Thread {
private Queue<Car> q = new LinkedList<Car>();

public void CreateQueue() {

Scanner input = new Scanner(System.in);

for (int i = 0; i < 2; i++) {
Car c = new Car();
System.out.println("Enter registration number: ");
String regNum = input.nextLine();
c.setRegNum(regNum);
q.offer(c);
}
}

public int getQueueSize() {
return q.size();
}

@Override
public void run() {
try {
while (q.size() != 0) {
synchronized (this) {
System.out.print("The car with registration number: ");
System.out.print(q.poll().getRegNum());
System.out
.println(" have passed the bridge from the north side.");
this.wait(2000);
notify();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}

}

主线程:

public class Main {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

SouthThread tSouthThread = new SouthThread();
NorthThread tNorthThread = new NorthThread();

tSouthThread.CreateQueue();
tNorthThread.CreateQueue();

System.out.println(tSouthThread.getQueueSize());

tSouthThread.start();
tNorthThread.start();


}

最佳答案

您基本上想要实现的似乎是一个在两个独立单元之间交替控制的系统,以便每个单元都有一些时间来处理,然后等待两秒(反之亦然)。

您可以通过两种主要方式实现这一目标:

  1. 使用中央控制
  2. 使用自主通信代理

第一种方法更容易一些。在这里,您有一个中央“主”组件,它负责协调谁获得处理时间并实现等待时间。对于这种方法,两个独立的单元甚至不必是线程:

public class South {
private Queue<Car> q = new LinkedList<Car>();

public void CreateQueue() { ... }

public void poll() {
System.out.print("The car with registration number: ");
System.out.print(q.poll().getRegNum());
System.out.println(" have passed the bridge from the South side.");
}
}

public class North {
private Queue<Car> q = new LinkedList<Car>();

public void CreateQueue() { ... }

public void poll() {
System.out.print("The car with registration number: ");
System.out.print(q.poll().getRegNum());
System.out.println(" have passed the bridge from the North side.");
}
}

// This is the "master" class
public class Main {
public static void main(String[] args) {
South south = new South();
North north = new North();

south.CreateQueue();
north.CreateQueue();

boolean done = false;
while (!done) {
try {
Thread.sleep(2000);
} (catch InterruptedException) { /* TODO */ }

north.poll();

try {
Thread.sleep(2000);
} (catch InterruptedException) { /* TODO */ }

south.poll();
}
}
}

请注意,这里的 North 和 South 不是从 Thread 继承的,也就是说,它们只是普通的旧对象。(但是,如果您的程序更复杂并且 North/South 只是其中的一部分,您可能希望将 Main(!) 设为一个单独的线程并将上面的 while 循环放在线程的 run 中方法,以便程序的其余部分可以同时运行。)

在第二种方法中,您没有这样的中央控制组件,但 North 和 South 都在各自独立的线程中运行。这就要求他们通过相互沟通来协调允许处理的人员。

public class SouthThread extends Thread {
protected Queue<Car> q = new LinkedList<Car>();
protected North north;

public void CreateQueue() { ... }
public void poll() { ... }

public void run() {
boolean done = false;
while (!done) {
// wait two seconds
try {
Thread.sleep(2000);
} (catch InterruptedException) { /* TODO */ }

// process one element from the queue
poll();

// notify the other thread
synchronized (north) {
north.notifyAll();
}

// wait until the other thread notifies this one
try {
synchronized (this) {
wait();
}
} (catch InterruptedException) { /* TODO */ }
}
}
}

public class NorthThread extends Thread {
protected Queue<Car> q = new LinkedList<Car>();
protected South south;

public void CreateQueue() { ... }
public void poll() { ... }

public void run() {
boolean done = false;
while (!done) {
// wait two seconds
try {
Thread.sleep(2000);
} (catch InterruptedException) { /* TODO */ }

// process one element from the queue
poll();

// notify the other thread
synchronized (south) {
south.notifyAll();
}

// wait until the other thread notifies this one
try {
synchronized (this) {
wait();
}
} (catch InterruptedException) { /* TODO */ }
}
}
}

public class Main {
public static void main(String[] args) throws Exception {
SouthThread tSouthThread = new SouthThread();
NorthThread tNorthThread = new NorthThread();

tSouthThread.north = tNorthThread;
tNorthThread.south = tSouthThread;

tSouthThread.CreateQueue();
tNorthThread.CreateQueue();

tSouthThread.start();
tNorthThread.start();
}
}

一个更笼统的评论:由于 North 和 South 似乎在做基本相同的事情,因此可能没有必要在两个单独的类中实现它们。相反,只有一个实现所需功能的类并将其实例化两次就足够了:

// We can combine the functionality of North and South 
// in a single class
public class NorthSouth {
public void CreateQueue() { ... }
public void poll() { ... }

// etc.
}

public class Main {
public static void main(String[] args) {
NorthSouth north = new NorthSouth();
NorthSouth south = new NorthSouth();

north.CreateQueue();
south.CreateQueue();

// etc.
}
}

关于java - 如何让两个线程等待并互相通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20614926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com