First, the basic concept of threading:
A thread is a different execution path within a program. There are different execution paths within the program, and each branch is a thread. Process: a static concept. A class file on the machine, an EXE file on the machine, which is called a process. The machine is actually running threads. window and so on. Linux and so on are multi-process, multi-threaded system. CPU execution is like this: The CPU speed is relatively fast, a second count hundreds of millions of times, it takes its own time into a small time slice, this time slice I execute you a while, then executes him a while, although has dozens of threads, does not matter, carries on this one meeting, executes that one meeting, the platoon next row all executes again, But for us people, because it's too fast, you look like a good multi-threaded execution at the same time. But in fact, at a point in time, this CPU has only one thread running. If the machine is a dual-CPU, or dual-core, it is indeed multithreaded.second, thread startup and creation:Example 1: Implementing the Runnable Interface:
PackageCom.cy.thread; Public classTestThread1 { Public Static voidMain (string[] args) {Runner1 R=NewRunner1 (); Thread T=NewThread (R); //starting a thread, the thread initiates must call the start () method of the thread class//The Start method notifies the CPU, I now have a new thread, Ah, is ready, you old man when there is time to hurry to give me a time slice. T.start (); for(inti=0; i<100; i++) {System.out.println ("Main Thread:-----" +i); } }}/*** After implementing the runnable, the JDK will know that this is a thread class. */classRunner1Implementsrunnable{@Override Public voidrun () { for(inti=0; i<100; i++) {System.out.println ("Runner1:" +i); } } }
Console
Example 2: Inheriting the Thread class:
PackageCom.cy.thread; Public classTestThread1 { Public Static voidMain (string[] args) {Runner1 R=NewRunner1 (); R.start (); for(inti=0; i<100; i++) {System.out.println ("Main Thread:-----" +i); } }}classRunner1extendsthread{@Override Public voidrun () { for(inti=0; i<100; i++) {System.out.println ("Runner1:" +i); } } }
Third, the state transition of the thread:New Thread () after a thread, does not execute immediately but into a ready state, because the CPU is very busy, may be executing other programs, wait until the CPU is empty, the CPU allocates time slices to execute (dispatch) this thread will go into the running state When the CPU is allocated to this thread for the time slice to run out, or for other reasons, the thread no longer executes, blocks, and enters the ready state again, until the thread's program is finished;four, the basic method of threading control:
IsAlive (): Determines whether the thread is still alive, that is, whether the thread has not been terminated, ready, running, blocking is alive. Ended up dead. The thread was created and not started. That's dead. GetPriority () Gets the priority number of the thread, and the higher the priority thread gets the more time the CPU executes. SetPriority () sets the priority value of the thread, and Thread.Sleep () Specifies the number of milliseconds to sleep on the current thread. Join () merges a thread. Yield () sharp sense, yielding the CPU, allowing other threads to execute while themselves into a ready state. Wait () The current thread enters the object's wait Pool;notify, Notifyall () wakes the object to one/all waiting threads in the wait pool.
View Code
Five, sleep method:
PackageCom.cy.thread;Importjava.util.Date; Public classTestinterrupt { Public Static voidMain (string[] args) {MyThread thread=NewMyThread (); Thread.Start (); Try { /*** in which thread call the sleep method, let the thread sleep. */Thread.Sleep (10000); } Catch(interruptedexception e) {e.printstacktrace (); } /*** Interrupt () Method: Thread threads interrupt when they are sleeping. * Here for example demo, let the child thread end. * But this is not the best way to end a child thread. */Thread.Interrupt (); }}classMyThreadextendsthread{@Override Public voidrun () { while(true) {System.out.println ("==="+NewDate () + "= = ="); Try{sleep (1000); } Catch(interruptedexception e) {return; } } }}View Code
Console print:
The above program provides a way to end the Mythread thread:
/*** Provides a way to end the thread: * Thread.flag = false; The run () method is no longer executed, and the thread ends when the Run method is finished. */classMyThread2extendsthread{BooleanFlag =true; @Override Public voidrun () { while(flag) {System.out.println ("==="+NewDate () + "= = ="); Try{sleep (1000); } Catch(interruptedexception e) {return; } } }}View Code
---------------
Java Multithreading (1) Basic Concepts of threading