
public class ThreadTest
{
public static void main(String[] args)
{
ThreadTest myThreadTest=new ThreadTest();
myThreadTest.go();
}
public void go()
{
FirstThread myThread1=new FirstThread();
SecondThread myThread2=new SecondThread();
ThirdThread myThread3=new ThirdThread();
FourthThread myThread4=new FourthThread();
FifthThread myThread5=new FifthThread();
myThread3.setPriority(Thread.MAX_PRIORITY);//设最高优先级,依然不会太影响,线程的执行,优先级最高,只是他运行的概率会更大点,但至于是不是能抢到cpu运行,也不一定,反正线程,就是存在太多不确定性
myThread1.start();
myThread2.start();
myThread3.start();
myThread4.start();
myThread5.start();
}
class MyThreadAction implements Runnable
{
public void run()
{
System.out.println("Thread "+"running");
}
}
class FirstThread extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
System.out.println("First thread running!---->"+i);
}
}
class SecondThread extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
System.out.println("Second thread running!---->"+i);
}
}
class ThirdThread extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
System.out.println("Third thread running!---->"+i);
}
}
class FourthThread extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
System.out.println("Fourth thread running!---->"+i);
好明 }
}
class FifthThread 芹此extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
System.out.println("Fifth thread running!---->"+i);
}
}
}
运行结果:
每次的运行结果,都不一样,主要就是由友首告于线程不确定性引起的。
public void run(int numSets,boolean debug) throws Exception {
ExecutorService executorService = null; //numSets 线程谨旁数目,debug线程开关
if (debug) {
executorService = Executors.newFixedThreadPool(1);
} else {
executorService = Executors.newFixedThreadPool(numSets);
}
futures.add(executorService.submit(new Callable
public void call() throws Exception {
//要做的事情
}
}));
}
for (Future
future.get();
}
//关闭
executorService.shutdownNow();
}
另外用户可以通过祥丛橡调郑李用Thread类的方法setPriority()来修改系统自动设定的线程优先级。