Edit: This is a question about Java.
Here is some code I copied (with a few added comments) from a video I'm watching (My question is about what's bolded):
Code:
import java.util.Random;
public class learningThreads implements Runnable
{
String name;
int time;
Random r = new Random();
public learningThreads(String x)
{
name = x;
time = r.nextInt(999);
}
public void run()
{
try
{
System.out.printf("%s is sleeping for %d\n", name, time);
Thread.sleep(time);
System.out.printf("%s is done\n", name);
}
catch(Exception e){}
}
public static void main(String[] args)
{
// The following creates threads but are not yet started to be used
Thread t1 = new Thread(new learningThreads("one"));
Thread t2 = new Thread(new learningThreads("two"));
Thread t3 = new Thread(new learningThreads("three"));
Thread t4 = new Thread(new learningThreads("four"));
// The following starts each thread above
t1.start();
t2.start();
t3.start();
t4.start();
}
}
At first, I thought the purpose of the nextInt() method is to give an integer to the object that calls it but then I noticed that instead of giving it a specific integer, the integer being set is an interval with that specific integer being the end of the interval. I was just hoping someone could explain that part to me in a little more detail.
Any input would be greatly appreciated!
Thanks in advance!
Bookmarks