我想以严格的顺序执行两个单独的线程,即" A-> B-> A-> B",谁知道怎么做?
我不希望序列之间有任何延迟(例如睡眠,产量)。
这是我写的一些代码但无法工作的代码:
public void onClick_start_thread_a(View v) {
logger.d("onClick_start_thread_a");
Runnable r = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (flag) {
try {
flag.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.d("Thread A!");
}
}
}
};
Thread t = new Thread(r);
t.start();
}
public void onClick_start_thread_b(View v) {
logger.d("onClick_start_thread_b");
Runnable r = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (flag) {
flag.notify();
logger.d("Thread B!");
}
}
}
};
Thread t = new Thread(r);
t.start();
}
onClick_start_thread_a和onClick_start_thread_b由两个不同的按钮触发,点击按钮后输出为:
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-619}[MainActivity:] Thread A!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-619}[MainActivity:] Thread A!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-619}[MainActivity:] Thread A!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-619}[MainActivity:] Thread A!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
01-11 22:49:40.705: D/THING(25877): {Thread:Thread-620}[MainActivity:] Thread B!
答案 0 :(得分:1)
public void onClick_start_thread_a(View v) {
logger.d("onClick_start_thread_a");
Runnable r = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (flag) {
System.out.println("Thread A!");
flag.notifyAll();
try
{
flag.wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
};
Thread t = new Thread(r);
t.start();
}
public void onClick_start_thread_b(View v) {
logger.d("onClick_start_thread_b");
Runnable r = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (flag) {
System.out.println("Thread B!");
flag.notifyAll();
try
{
flag.wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
};
Thread t = new Thread(r);
t.start();
}
答案 1 :(得分:0)
你可能想要的是某种信号量和信号传导。
Start Threads A and B
B - waits on semaphore
A - does stuff
A - when finished A signals B
A - waits on semaphore
B - does stuff
B - when finished B signals A
...... repeat until done
查看文档here
答案 2 :(得分:0)
如果它是一个像你这样的简单案例(只有2个线程),那么我认为你可以使用一个简单的布尔值(在Java的情况下为AtomicBoolean)
1)设置布尔值(让它将其判断为decider)为false
2)启动两个线程
3)线程1将具有:
while(true) {
if(decider.get()) {
// do stuff
decider.set(false);
}
}
另一个:
while(true) {
if(!decider.get()) {
// do stuff
decider.set(true);
}
}
虽然(true)(或其他一些条件)似乎有点开销,所以你可以在两个if语句中添加一个锁(Lock#lock()将等待锁被释放)
如果你想将它推广到更多线程,你只需要用可能需要更多状态的东西替换布尔值。每个线程可能有一个ID(从0到N),只有当invocationNr模数为nrOfThreads == ID时才会输入if。