java multithreading example

World Java

import java.io. *;

//Multithreaded programming public class MultiThread   { public static void main(String args[]) { System.out.println("I am the main thread!"); //Create a thread instance below thread1 ThreadUseExtends thread1=new ThreadUseExtends(); //When creating thread2, the THhreadUseRunnable class instance that implements the Runnable interface is used as the parameter Thread thread2=new Thread(new ThreadUseRunnable(),"SecondThread"); thread1.start();//Start thread thread1 to make it ready // thread1.setPriority(6);//Set the priority of thread1 to 6 //The priority will determine which thread in the ready state will occupy the CPU first to start running when the cpu is vacated.//The priority range is 1 to 10, MIN_PRIORITY, MAX_PRIORITY ,NORM_PAIORITY //The new thread inherits the priority of the parent thread that created her. The parent thread usually has a normal priority, which is 5NORM_PRIORITY System.out.println("The main thread will hang for 7 seconds!"); try { Thread.sleep(7000) ;//Main thread hangs for 7 seconds } 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
catch (InterruptedException e) { return; } System.out.println("Back to the main thread!"); if(thread1.isAlive()) {   thread1.stop();//If thread1 still exists, kill him System.out.println("Thread1 sleeps too long, the main thread killed thread1!"); } else System.out.println("The main thread did not find thread1, thread1 has woken up and the sequence execution is over!"); thread2. start();//Start thread2 System.out.println("The main thread will be suspended for 7 seconds again!"); try { Thread.sleep(7000);//The main thread is suspended for 7 seconds } catch (InterruptedException e) { return; } System.out.println("Back to the main thread again!"); if(thread2.isAlive()) {   thread2.stop();//If thread2 still exists, kill him System.out.println ("thread2 sleeps too long, the main thread kills thread2!"); 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
} else System.out.println("The main thread did not find thread2, thread2 has woken up and the sequence execution is over!"); System.out.println("Press any key to continue at the end of the program!"); try { System.in.read (); } catch (IOException e) { System.out.println(e.toString()); } 
 
 
 
 
 
 
 
 
 
 


}//main}//MultiThread 



class ThreadUseExtends extends Thread //By inheriting the Thread class and implementing its abstract method run() //Create an instance of this Thread subclass at the appropriate time to implement the multi-threading mechanism //After a thread starts (that is, it enters the ready state) Once the CPU is obtained, its run() method will be called automatically { 
 
 
 


ThreadUseExtends(){}//构造函数 
public void run() 
{ 
System.out.println("我是Thread子类的线程实例!"); 
System.out.println("我将挂起10秒!"); 
System.out.println("回到主线程,请稍等,刚才主线程挂起可能还没醒过来!"); 
try 
{ 
sleep(10000);//挂起5秒 
} 
catch (InterruptedException e) 
{ 
return; 
} 
//如果该run()方法顺序执行完了,线程将自动结束,而不会被主线程杀掉 
//但如果休眠时间过长,则线程还存活,可能被stop()杀掉 
} 
}



class ThreadUseRunnable implements Runnable 
//通过实现Runnable接口中的run()方法,再以这个实现了run()方法的类 
//为参数创建Thread的线程实例 
{ 
//Thread thread2=new Thread(this); 
//以这个实现了Runnable接口中run()方法的类为参数创建Thread类的线程实例 
ThreadUseRunnable(){}//构造函数 
public void run() 
{ 
System.out.println("我是Thread类的线程实例并以实现了Runnable接口的类为参数!"); 
System.out.println("我将挂起1秒!"); 
System.out.println("回到主线程,请稍等 jn0-120 e20-040 ,刚才主线程挂起可能还没醒过来!"); 
try 
{ 
Thread.sleep(1000);//挂起5秒 
} 
catch (InterruptedException e) 
{ 
return; 
} 
//如果该run()方法顺序执行完了,线程将自动结束,而不会被主线程杀掉 
//但如果休眠时间过长,则线程还存活,可能被stop()杀掉 
}


} 
//该程序可做的修改如改休眠时间或优先级setPriority()

posted on 2007-09-19 11:13   java2java  阅读(51856)   评论(15)    编辑    收藏

评论

#  re: java一个多线程的经典例子   2007-09-19 11:19  千里冰封
呵呵,有意思.其实JAVA的线程确实挺灵活的   回复   更多评论  
  

#  re: java一个多线程的经典例子   2007-09-19 11:37  Unmi
代码有点乱,不过意思大概是这样子的  
Win32下是主线程使用API方法  
WaitForSingleObject() 等待单一线程  
或者  
WaitForMultiObject() 等待多个线程  
等待用户线程执行完的信号,退出主线程  

如果你能够把Object的notify()或notifyAll(),三个wait()方法应用上,那么你对线程以及线程间的事件对象(win32的名词)就有更透彻的理解了  

多线程的实现机制其实在Java中与用Win32 API是完全一致的。   回复   更多评论  
  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326514272&siteId=291194637