Multitasking and Multithreading: multi-tasking is a relationship between applications at the system level, and multithreading is a relationship between multiple functions of an application-level app
There are two ways to create a thread:
Implementing the Runnable Interface
Class Sunclass {}
Class Sumthread extends Sumclass imlements runnable{}
public class **{public void Main () {Sumthread st=new sumthread (); thread t=new thread (St, "ThreadName"); T.start ();}}
Inheriting the thread class (directly inheriting the thread class, or using anonymous nested classes)
classSumthread extends thread{Private Longlength; PublicSumthread (Longlength,string name) {super (name); This. length=length; } Public voidrun () {Longtemp=0; for(intI=1; i<=length;i++){ Try{Thread.Sleep (int) (Math.random () *Ten)); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } temp+=i; } System. out. println (Thread.CurrentThread () +"Zonghe:"+temp); }} Public classThread1 { Public Static voidMain (string[] args) {System. out. println ("Threads"); System. out. println (Thread.CurrentThread ()); Sumthread St1=NewSumthread ( Max,"Thread 1"); St1.start (); NewThread ("Thread 2"){ intLength= Max; Public voidrun () {Longtemp=0; for(intI=1; i<=length;i++){ Try{Thread.Sleep (int) (Math.random () *Ten)); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } temp+=i; } System. out. println (Thread.CurrentThread () +"Zonghe:"+temp); }}.start ();}}
Threads for Java