gpt4 book ai didi

java - java中两个线程如何相继访问同一个对象的两个方法

转载 作者:行者123 更新时间:2023-12-01 15:10:51 25 4
gpt4 key购买 nike

Possible Duplicate:
Trying to loop 3 threads in a specific order everytime

我想从两个线程依次访问同一对象的两种不同方法。这是我的代码,

public class ThreadCoordination
{
private Thread threadSayHello;
private Thread threadSayWorld;
private boolean threadSayWorldStarted = false;

public ThreadCoordination()
{
createThreads();
}

private void createThreads()
{
threadSayWorld = new Thread(new Runnable()
{
public void run()
{
try
{
// while (true)
{
sayWorld();
}
}
catch (InterruptedException ex)
{}
}
});

threadSayHello = new Thread(new Runnable()
{
public void run()
{
try
{
// while (true)
{
sayHello();

if (!threadSayWorldStarted)
{
threadSayWorldStarted = true;
threadSayWorld.start();
}
}
}
catch (InterruptedException ex)
{}
}
});

threadSayHello.start();
}

private synchronized void sayHello() throws InterruptedException
{
System.out.print("Hello ");
}

private synchronized void sayWorld() throws InterruptedException
{
System.out.println("World!");
}

public static void main(String[] args)
{
new ThreadCoordination();
}
}

如果我取消注释调用 while(true),那么我会期待这样的输出,

Hello World!
Hello World!
Hello World!
Hello World!
...

请指导我该怎么做。拉贾。

我不知道是否可以编辑已关闭的帖子。我只是想发布我所知道的解决方案。

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class SequenceAccess
{
private ReentrantLock myLock;
private Condition ensureSequence;
private int sequenceNo = 1;

public SequenceAccess()
{
myLock = new ReentrantLock();
ensureSequence = myLock.newCondition();

startThreads();
}

private void startThreads()
{
new Thread(new Runnable()
{
public void run()
{
try
{
while (true)
method1();
}
catch (InterruptedException ex)
{}
}
}).start();

new Thread(new Runnable()
{
public void run()
{
try
{
while (true)
method2();
}
catch (InterruptedException ex)
{}
}
}).start();

new Thread(new Runnable()
{
public void run()
{
try
{
while (true)
method3();
}
catch (InterruptedException ex)
{}
}
}).start();
}

private void method1() throws InterruptedException
{
myLock.lock();
try
{
while (sequenceNo != 1)
ensureSequence.await();

sequenceNo = 2;
System.out.println("Method 1");
ensureSequence.signalAll();
}
finally
{
myLock.unlock();
}
}

private void method2() throws InterruptedException
{
myLock.lock();
try
{
while (sequenceNo != 2)
ensureSequence.await();

sequenceNo = 3;
System.out.println("Method 2");
ensureSequence.signalAll();
}
finally
{
myLock.unlock();
}
}

private void method3() throws InterruptedException
{
myLock.lock();
try
{
while (sequenceNo != 3)
ensureSequence.await();

sequenceNo = 1;
System.out.println("Method 3");
ensureSequence.signalAll();
}
finally
{
myLock.unlock();
}
}

public static void main(String[] args)
{
new SequenceAccess();
}
}

最佳答案

JVM 不保证线程执行的顺序

事实上,通过运行 hello 线程直至其终止,然后运行 ​​world 线程直至其终止(按照您编写的程序),可以完全满足 JVM 规范

您将需要引入某种在两个线程之间共享的 token ,然后让该 token 来回穿梭。

token 可以像 boolean 值一样简单,如果已输出 hello,则为 true;如果已输出 world,则为 false。

然后,每个线程都必须旋转(或等待条件 - 通过条件获得更好的性能),直到 boolean 值与其预期状态匹配。

我建议您阅读最优秀的《Java 并发实践》一书

关于java - java中两个线程如何相继访问同一个对象的两个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12388012/

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