java多线程问题(50分)

  • 主题发起人 主题发起人 hanxuejian
  • 开始时间 开始时间
H

hanxuejian

Unregistered / Unconfirmed
GUEST, unregistred user!
我根据书上的例子写了一个程序,无法编译,请有识之士指教一二。
public class Thread02
{
public static void main(String[] args)
{
Thread myThreadA=new Thread(new MyThread(),"threadA");
Thread myThreadB=new Thread(new MyThread(),"threadB");
myThreadA.start();
myThreadB.start();
try
{
Thread.currentThread().sleep(1000);
}
catch(InterruptedException e)
{
}
System.out.println(Thread.currentThread());
}
classdo
Noting
{
}
class MyThread extendsdo
Noting implements Runnable
{
public void run()
{
for(int cnt=0;cnt<5;cnt++)
{
System.out.println(Thread.currentThread()+"的运行次数:"+cnt);
try
{
Thread.currentThread().sleep((int)(Math.random()*100));
}
catch(InterruptedException e)
{
}
}
}
}
}
 
参考一下这个:
package nettest;
public class threadtest
{
public threadtest()
{
Thread myThreadA=new Thread(new MyThread(),"threadA");
Thread myThreadB=new Thread(new MyThread(),"threadB");
myThreadA.start();
myThreadB.start();
try
{
Thread.currentThread().sleep(1000);
}
catch(InterruptedException e)
{
}
System.out.println(Thread.currentThread());
}
public static void main(String args[]) {
threadtest t = new threadtest();
}
classdo
Noting
{
}
class MyThread extendsdo
Noting implements Runnable
{
public void run()
{
for(int cnt=0;cnt<5;cnt++)
{
System.out.println(Thread.currentThread()+"的运行次数:"+cnt);
try
{
Thread.currentThread().sleep((int)(Math.random()*100));
}
catch(InterruptedException e)
{
}
}
}
}
}
可以运行的
 
接受答案了,谢谢!
 

Similar threads

W
回复
5
查看
240
wait_for_love
W
回复
8
查看
279
小猪
回复
2
查看
461
蛮牛野蛮牛
后退
顶部