import java.io.*;
public class Threadmain
{
public static void main(String[] args)
{
int i = 0;
new TestThread(i).start();
new TestThread(i).start();
new TestThread(i).start();
new TestThread(i).start();
new TestThread(i).start();
new TestThread(i).start();
new TestThread(i).start();
new TestThread(i).start();
new TestThread(i).start();
new TestThread(i).start();
new TestThread(i).start();
while(true)
{
//System.out.println("main():"+Thread.currentThread().getName()+" is running " + i++);
}
}
}
多线程类:
import java.io.*;
public class TestThread extends Thread {
private int i;
public TestThread(int i)
{
this.i = i;
}
public void run()
{
while(true)
{
System.out.println("thread():"+Thread.currentThread().getName()+" is running " + i++);
}
}
}
以下是:这个I可以是复用的
import java.io.*;
public class Threadmain
{
public static void main(String[] args)
{
new TestThread().start();
new TestThread().start();
new TestThread().start();
new TestThread().start();
while(true)
{
//System.out.println("main():"+Thread.currentThread().getName()+" is running " + i++);
}
}
}
import java.io.*;
public class TestThread extends Thread {
static int i = 1;
public void run()
{
while(true)
{
System.out.println("thread():"+Thread.currentThread().getName()+" is running " + i++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
随机数
import java.util.*;
Random random2 = new Random();
System.out.println(random2.nextInt(100));