0

I need to create 2 threads to roll a dice and print in dos.here is my code: --class thread--

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 %s\n", taskName,"terminated prematurely due to interruption");
        }
        System.out.printf("%s done sleeping\n",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();



    }
}

I get an error in the main that says i needs to have an integer also. I am new in java and programming ,any help would be very appreciated. Thanks

user3078577
  • 19
  • 1
  • 5

1 Answers1

2

your thread class has to implement Runnable public class thread1 implements Runnable so it will be able to run that way

look here for the two ways to create a thread: "implements Runnable" vs. "extends Thread"

Community
  • 1
  • 1