1. Thread hibernation
public static void sleep (long Millis)
Public classThreadsleepextendsThread {@Override Public voidrun () { for(intx = 0; x < 100; X + +) {System.out.println (GetName ()+ ":" + x + ", Date:" +NewDate ()); //Sleeping//I'm sleepy, I have a little rest for 1 seconds. Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } } }}/** Thread hibernation * public static void sleep (long Millis)*/ Public classThreadsleepdemo { Public Static voidMain (string[] args) {threadsleep ts1=NewThreadsleep (); Threadsleep ts2=NewThreadsleep (); Threadsleep Ts3=NewThreadsleep (); Ts1.setname ("Brigitte"); Ts2.setname ("Lin Chi-ling"); Ts3.setname ("Lin"); Ts1.start (); Ts2.start (); Ts3.start (); }}
2, join thread: Join the thread execution end before executing other threads
Public final void Join (): Waits for the thread to terminate before executing another thread
Public classThreadjoinextendsThread {@Override Public voidrun () { for(intx = 0; x < 100; X + +) {System.out.println (GetName ()+ ":" +x); } }}/** Public final void Join (): Waits for the thread to terminate. */ Public classThreadjoindemo { Public Static voidMain (string[] args) {threadjoin tj1=NewThreadjoin (); Threadjoin tj2=NewThreadjoin (); Threadjoin tj3=NewThreadjoin (); Tj1.setname ("Liyuan"); Tj2.setname ("Li Shimin"); Tj3.setname ("Li Yuan Pingdu pa"); //execute "Liyuan" before executing other threadsTj1.start (); Try{tj1.join (); } Catch(interruptedexception e) {e.printstacktrace (); } tj2.start (); Tj3.start (); }}
3. Comity Thread: Pauses the currently executing thread object and executes other threads.
public static void Yield (): Pauses the currently executing thread object and executes other threads.
The execution of multiple threads is more harmonious, but it cannot be guaranteed by one person at a time.
Public classThreadyieldextendsThread {@Override Public voidrun () { for(intx = 0; x < 100; X + +) {System.out.println (GetName ()+ ":" +x);
//Invoke comity threading method Thread.yield (); } }}/** public static void Yield (): Pauses the currently executing thread object and executes other threads. * Make multiple threads more harmonious, but not guaranteed by one person at a time. */ Public classThreadyielddemo { Public Static voidMain (string[] args) {Threadyield ty1=NewThreadyield (); Threadyield Ty2=NewThreadyield (); Ty1.setname ("Thread 1"); Ty2.setname ("Thread 2"); Ty1.start (); Ty2.start (); }}
4. Background thread (daemon thread): After the main thread ends, the daemon thread will also terminate
Public final void Setdaemon (Boolean on): marks the thread as either a daemon thread or a user thread.
The difference between a daemon thread and a generic thread:
1, the daemon thread is main die, when main exits, it terminates, and the normal thread is stopped at the end of the task execution.
2. The Java virtual machine automatically leaves after all of its non-daemon threads have left. The daemon thread is used to serve the user thread, and if no other user thread is running, there is no service object and there is no reason to go on.
Public classThreaddaemonextendsThread {@Override Public voidrun () { for(intx = 0; x < 100; X + +) {System.out.println (GetName ()+ ":" +x); } }}/** Public final void Setdaemon (Boolean on): marks the thread as either a daemon thread or a user thread. * When a running thread is a daemon thread, the Java virtual machine exits. The method must be called before the thread is started. * * Game: Tank wars. */ Public classThreaddaemondemo { Public Static voidMain (string[] args) {Threaddaemon TD1=NewThreaddaemon (); Threaddaemon TD2=NewThreaddaemon (); Td1.setname ("Guan Yu"); Td2.setname ("Zhang Fei"); //Set Harvest ThreadTd1.setdaemon (true); Td2.setdaemon (true); //Daemon thread must be set Td1.start () before threads start ; Td2.start (); Thread.CurrentThread (). SetName ("Liu Bei"); for(intx = 0; x < 5; X + +) {System.out.println (Thread.CurrentThread (). GetName ()+ ":" +x); } }}
5. Terminating Threads
Public final void Stop (): Let the thread stop, obsolete, but can also be used.
public void Interrupt (): Middle break thread. Terminates the state of the thread and throws a interruptedexception.
Public classThreadstopextendsThread {@Override Public voidrun () {System.out.println ("Start execution:" +NewDate ()); //I'm going to take 10 seconds off, kiss, don't disturb me! Try{Thread.Sleep (10000); } Catch(interruptedexception e) {//e.printstacktrace ();SYSTEM.OUT.PRINTLN ("Thread has been terminated"); } System.out.println ("End Execution:" +NewDate ()); }}/** Public final void Stop (): Let the thread stop, obsolete, but can also be used. * public void Interrupt (): Middle Thread. Terminates the state of the thread and throws a interruptedexception. */ Public classThreadstopdemo { Public Static voidMain (string[] args) {threadstop ts=NewThreadstop (); Ts.start (); //If you don't wake up in three seconds, I'll kill you. Try{Thread.Sleep (3000); //ts.stop ();Ts.interrupt (); } Catch(interruptedexception e) {e.printstacktrace (); } }}
6. Priority settings for threads
The thread default priority is 5.
The range of thread priorities is: 1-10.
High thread priority only indicates that the thread gets a high chance of CPU time slices, but only when the number of times is higher, or multiple runs, can you see the better effect.
To set the thread priority method:
Public final int getpriority (): Returns the priority of the thread object
Public final void setpriority (int newpriority): Change the priority of a thread
IllegalArgumentException: Illegal parameter exception.
The exception thrown indicates that an invalid or incorrect argument was passed to the method
Public classThreadPriorityextendsThread {@Override Public voidrun () { for(intx = 0; x < 100; X + +) {System.out.println (GetName ()+ ":" +x); } }} Public classThreadprioritydemo { Public Static voidMain (string[] args) {threadpriority TP1=Newthreadpriority (); ThreadPriority TP2=Newthreadpriority (); ThreadPriority TP3=Newthreadpriority (); Tp1.setname ("Oriental Undefeated"); Tp2.setname ("Not a group of Yue"); Tp3.setname ("林平"); //gets the default priority, the default priority of the thread is 5//System.out.println (Tp1.getpriority ()); //System.out.println (Tp2.getpriority ()); //System.out.println (Tp3.getpriority ()); //Set thread priority, error: Thread priority range: 1-10//tp1.setpriority (100000); //set the correct thread priorityTp1.setpriority (10); Tp2.setpriority (1); Tp1.start (); Tp2.start (); Tp3.start (); }}
Java Learning Notes---multithreading (Control of multithreading)