我需要创建两个线程来掷骰子并在dos中打印。下面是我的代码:--类线程
package dice.application;
enter code here
import java.util.Random;
public class thread1
{
private final int sleepTime;
private final String taskName;
private final int fdice;
private final static Random generator=new Random();
Random randomdie = new Random();
public thread1(String name,int dice)
{
taskName=name;
sleepTime=generator.nextInt(5000);
dice=1+randomdie.nextInt(6);
fdice=dice;
}
public void run()
{
try
{
System.out.printf("%s going to sleep for %d milliseconds.n",taskName,sleepTime);
Thread.sleep(sleepTime);
System.out.printf("%s rolled : ",fdice);
}
catch(InterruptedException exception)
{
System.out.printf("%s %sn", taskName,"terminated prematurely due to interruption");
}
System.out.printf("%s done sleepingn",taskName);
}
}
public class DiceApplication {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic hereThread player1=new Thread(new PrintTask("roll1"));
Thread player1=new Thread(new thread1("",));
player1.start();
}
}
我得到一个主要的错误,说我也需要一个整数。我是java和编程的新手,如果有任何帮助,我将不胜感激。感谢
您的线程类必须实现Runnable public class thread1 implements Runnable
,这样它才能以这种方式运行
在这里查找创建线程的两种方法:;实现Runnable";与";扩展线程";