Java线程连接方法
在我的程序中,线程T1生成一个新线程T2并在该线程上调用join(即T2.join),而这个新生成的线程T2在T1上调用join(即T1.join)。这会导致线程阻塞。如何克服这一问题。 我的节目Java线程连接方法,java,Java,在我的程序中,线程T1生成一个新线程T2并在该线程上调用join(即T2.join),而这个新生成的线程T2在T1上调用join(即T1.join)。这会导致线程阻塞。如何克服这一问题。 我的节目 public class PositiveNegativeNumberProducerV1 { static Thread evenThread, oddThread; public static void main(String[] args) { oddThre
public class PositiveNegativeNumberProducerV1 {
static Thread evenThread, oddThread;
public static void main(String[] args) {
oddThread = new Thread(new OddProducer(evenThread), "oddThread");
oddThread.start();
}
}
class EvenProducer implements Runnable {
Thread t;
EvenProducer(Thread t) {
this.t= t;
}
public void run() {
for(int i=1; i<=100; i++) {
if(i%2==0) {
System.out.println("i = "+i+":"+Thread.currentThread().getName());
try {
System.out.println("Now join will be called on "+t.getName()+" by thread "+Thread.currentThread().getName());
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
class OddProducer implements Runnable {
Thread t;
OddProducer(Thread t) {
this.t= t;
}
public void run() {
for(int i=1; i<=100; i++) {
if(i%2!=0) {
System.out.println("i = "+i+":"+Thread.currentThread().getName());
try {
if(t==null) {
t = new Thread(new EvenProducer(PositiveNegativeNumberProducerV1.oddThread), "evenThread");
t.start();
}
if(t.isAlive()) {
System.out.println("evenThread is alive and join will be called on "+t.getName()+" by thread "+Thread.currentThread().getName());
t.join();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
公共类PositiveNegativeNumberProducerV1{
静态线程evenThread,oddThread;
公共静态void main(字符串[]args){
oddThread=新线程(新OddProducer(evenThread),“oddThread”);
oddThread.start();
}
}
类实现Runnable{
螺纹t;
均匀度(螺纹t){
t=t;
}
公开募捐{
对于(int i=1;i),如果您只想同步输出:1、2、3、4……那么就不应该使用连接(等待线程终止,即离开run方法)。考虑使用信号量对象上的WaIT()和NoTIFYY()对。
Object sema = new Object();
new Thread( new Runnable() {
@Override
public void run()
{
for ( int i = 1; i <= 100; i++ )
{
if ( i % 2 == 0 )
{
try
{
System.out.println( "Going to wait for the odd thread - "
+ Thread.currentThread().getName());
synchronized (sema)
{
sema.wait();
}
System.out.println( "i = " + i + ":" + Thread.currentThread().getName());
System.out.println( "Going to notify the odd thread - "
+ Thread.currentThread().getName());
synchronized (sema)
{
sema.notify();
}
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
}
}
}
}, "Even").start();
new Thread( new Runnable() {
@Override
public void run()
{
for ( int i = 1; i <= 100; i++ )
{
if ( i % 2 != 0 )
{
System.out.println( "i = " + i + ":" + Thread.currentThread().getName());
try
{
System.out.println( "Going to notify the even thread"
+ Thread.currentThread().getName());
synchronized (sema)
{
sema.notify();
}
System.out.println( "Going to wait for the even thread"
+ Thread.currentThread().getName());
synchronized (sema)
{
sema.wait();
}
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
}
}
}
}, "Odd").start();
Object sema=new Object();
新线程(newrunnable()){
@凌驾
公开募捐
{
对于(int i=1;我不这么做;这就是你克服它的方法。想象两个人想穿过同一扇门,每个人都等着另一个人穿过,然后再穿过自己——这就是僵局的定义。Boris你的句子缺少一个动词。@GhostCat动词是给失败者的。至少根据苹果自动更正。但是Bor就在那里,那根本不可能。所以答案是退一步,找出一个解决方案,在不进行这种相互连接的情况下提供你所需要的。鲍里斯:至少你的苹果产品正确地做了@的事情。Chrome/Android不适合我。