I. Basic concepts of threading
1. Line City a sequence control flow within a program.
2.Java threads are implemented through the Java.lang.Thread class.
3.VM starts with a thread defined by the main method {public static void main (args[] String)}.
4. You can create new threads by creating a new thread instance.
5. Each thread completes its operation by means of the method run () that corresponds to a particular thread object, and the method run () is called the thread body.
6. Start a thread by calling the start () method of the thread class.
Note: Multi-process (multiple tasks (Programs) can be run concurrently in the operating system), multithreading (multiple sequential streams executing in the same application).
Two. Creation of threads
1. Define thread class, implement Runnable interface (recommended, more flexible):
Thread thread = new Threa (target); Target is the Runnable interface type;
There is only one method in runnable: public void Run (); Used to define the thread run body.
* enables the runnable interface to provide shared data for multiple threads.
The static method of thread can be used in the Run method definition of the class that implements the Runnable interface:
public static thread CurrentThread () gets a reference to the current thread.
Public classThreadtheory { Public Static voidMain (String args[]) {Runner1 runner1=NewRunner1 (); Thread Thread=NewThread (Runner1); Thread.Start (); for(inti=0;i<10;i++) {System.out.println ("Main Thread:" +i); } }} Public classRunner1ImplementsRunnable { Public voidrun () { for(inti=0;i<10;i++) {System.out.println ("Runner1:" +i); } }}View Code
2. Inherit the thread class, overriding the Run method:
Class MyThread extends thread{
public void Run () {...}
}
The object of the class is then generated:
MyThread myThread = new myThread (...);
Public classThreadtheory { Public Static voidMain (String args[]) {Runner2 Runner2=NewRunner2 (); Runner2.start (); //runner2 itself is a thread, no more new thread (); for(inti=0;i<10;i++) {System.out.println ("Main Thread:" +i); } }} Public classRunner2extendsThread { Public voidrun () { for(inti=0;i<10;i++) {System.out.println ("Runner2:" +i); } }}View Code
Three. Basic methods of threading control
Sleep (Thread.Sleep (long Millis))
Sleep interruptedexception
-
Sleeps (suspends execution) the currently executing thread within the specified number of milliseconds. The thread does not lose ownership of any monitors.
-
-
Parameters:
-
millis-The sleep time in milliseconds.
-
Thrown:
-
InterruptedException -If another thread interrupts the current thread. When the exception is thrown, the
interrupt state of the current thread is cleared.
Interrupt (Thread.Interrupt ())
Interrupt ()
-
The thread is disconnected.
If the current thread does not interrupt itself (which in any case is allowed), then the thread's method is checkAccess called, which may be thrown SecurityException .
If a thread is blocked in a call to a Object class wait() , wait(long) or wait(long, int) method, or a class,, join() join(long) join(long, int) , sleep(long) or sleep(long, int) method, its break state will be cleared and it will also receive one InterruptedException .
If the thread is blocked in an I/O operation on an interruptible channel, the channel is closed, the interrupt state of the thread is set, and the thread receives one ClosedByInterruptException .
If the thread is blocked in one Selector , the interrupt state of the thread is set, it is returned immediately from the selection operation, and may have a value other than 0, as if the selector was called wakeup .
If none of the previous conditions are saved, the interrupt state of the thread is set.
-
-
Thrown:
-
SecurityException -If the thread cannot be modified by the current thread
Note: A comfortable way to end a thread (denial of violence):
Public classThreadtheory { Public Static voidMain (String args[]) {MyThread MyThread=NewMyThread (); Mythread.start (); Try{Thread.Sleep (10000); }Catch(Interruptedexception e) {} mythread.stopthread (); //to end a thread with a flag }} Public classMyThreadextendsThread {Private BooleanFlag =true; Public voidStopthread () { This. Flag =false; } Public voidrun () { while(flag) {System.out.println ("= = = Current Time" +NewDate () + "= = ="); Try{sleep (1000); }Catch(interruptedexception e) {return; } } }}View Code
Basic knowledge of threading thread