线程没有按预期进行

时间:2016-06-23 08:24:50

标签: java multithreading

我尝试使用多线程概念模拟400米比赛。在这个应用程序中,共创建了5组,并按国家名称命名,每组应该有2名玩家。第一名玩家可以在前200米跑由小组中的下一个玩家和胜利者小组必须宣布。因为我是多线程概念的初学者,我试图实现第一个200米的过程,而不涉及下一个200米的过程。但运行代码不打印任何东西console.please帮我解决这个问题......

RelayDemo.java

public class RelayDemo implements Runnable
{
    public void race()
    {
        for(int i=0;i<200;i++)
        {
            if(Thread.currentThread().getName()=="rakesh"||Thread.currentThread().getName()=="nathan"||Thread.currentThread().getName()=="max"||Thread.currentThread().getName()=="villiers"||Thread.currentThread().getName()=="decapro")
        {
            System.out.println("The distance covered by"+Thread.currentThread().getName()+"is "+i);
        }

    }
}
public void run()
{
    this.race();
}
}

ExcecuteRelay.java

public class ExcecuteRelay {

public static void main(String[] args) 
{
    RelayDemo r=new RelayDemo();
    ThreadGroup t1=new ThreadGroup("india");
    ThreadGroup t2=new ThreadGroup("australia");
    ThreadGroup t3=new ThreadGroup("usa");
    ThreadGroup t4=new ThreadGroup("africa");
    ThreadGroup t5=new ThreadGroup("france");
    Thread r1=new Thread(t1,r,"rakesh");
    Thread r2=new Thread(t1,r,"kishore");
    Thread r3=new Thread(t2,r,"nathan");
    Thread r4=new Thread(t2,r,"johnson");
    Thread r5=new Thread(t3,r,"max");
    Thread r6=new Thread(t3,r,"tim");
    Thread r7=new Thread(t4,r,"villiers");
    Thread r8=new Thread(t4,r,"duminy");
    Thread r9=new Thread(t5,r,"decapro");
    Thread r10=new Thread(t5,r,"lues");
    r1.start();
    r2.start();
    r3.start();
    r4.start();
    r5.start();
    r6.start();
    r7.start();
    r8.start();
    r9.start();
    r10.start();
        // TODO Auto-generated method stub

}

}

0 个答案:

没有答案