What is a process and what is a thread?
In short, a process can be seen as an instance of a running program. Threads can be seen as an execution path within a process.
Each process has a separate code and data space (the process context), and the memory and data of the program can be shared in the same process. So switching between threads is less costly, and switching between processes is costly.
Multi-Process : ability to run multiple tasks concurrently in the operating system ( program ) ;
Multithreading : There are multiple concurrent tasks in the same process executing
There are two ways to create a thread.
way one: implement interface, overriding run method thread runnable subclass instances to encapsulate , call thead Span style= "FONT-SIZE:19PX;" >start method to start.
Way two: inheritance Thread class, overriding Run method, call Start () causes the thread to be in an "executable/runnable" state, waiting for the virtual machine to dispatch
when you start a thread, the created instance cannot be called directly, overriding the Run method, or not to start multithreading. Instead , the start method is called.
Run method is called the thread body method, and the thread is CPU scheduled, automatic execution, the same thread object can only be called once Start method. theRunnable interface can share data for multiple instances of a thread, regardless of whether the thread ends.
Method:
long getId () : Returns the identifier of the thread.
String getName () : Returns the name of the thread.
void interrupt () : Medium Break thread.
boolean isAlive () : Tests whether the thread is active.
boolean Isdaemon () : Tests whether the thread is a daemon thread.
booleanisinterrupted () : Tests whether the thread has been interrupted.
void Setdaemon (Booleanon) : Marks the thread as either a daemon thread or a user thread.
voidsetname (String name) : Change the name of the thread to the parameter name same.
static voidsleep (long Millis) : Lets the currently executing thread hibernate (suspends execution) within the specified number of milliseconds, which is affected by the accuracy and accuracy of the system timer and scheduler.
void Start () : Causes the thread to start executing; Java The virtual machine calls the thread's Run method.
static Voidyield () : Pauses the currently executing thread object and executes other threads
Two ways to break a thread:
method One: Set a flag bit in the thread class , through the other threads ( e.g. main thread ) to modify the value of this flag bit in the , to achieve the purpose of stopping the thread . typically in sub-threading Run () you need to add an exit condition in the loop if (flag) {.... break;}
method Two: Thread object invocation Interrupt () method.
(1) the thread to be interrupted is blocked in the sleep ()/wait ()/join ( ) on , will trigger a Interruptedexception, need to be Catch processing in a statement ;
(2)This is not the case,can be usedthread.interrupted ()to get the status of whether it is interrupted,if it was interrupted,,the Breakout,Follow-up statements are no longer executed;Note:after the method is called,will clear"Interrupt Flag",next time you call back,will getfalse
Scheduling of Threads:
thread sleep:Thread.Sleep (longmillis) throws Interruptedexception method, which causes the thread to go to a blocking state. the millis parameter sets the time to sleep, in milliseconds. When sleep is over, it becomes ready (Runnable) state.
Thread Concessions: Thread.yield () method, pausing the currently executing thread object and giving the execution opportunity to the same or higher priority thread.
Thread join: The Join () Throwsinterruptedexception method calls the join () method of another thread in the current thread, then the current thread goes into the waiting state until another thread finishes running, and the current thread is then turned into a ready state by blocking.
Thread waits: The Wait () throws Interruptedexception method in the object class causes the current thread to wait until the other thread calls the Notify () method of this object or the Notifyall () Wake method.
Thread Wake: The Notify () method in the object class that wakes up a single thread waiting on this objects monitor. If all threads are waiting on this object, then one of the threads is chosen to wake up. The choice is arbitrary. The Notifyall () method in the object class wakes up all the threads that are waiting on this objects monitor.
State transitions for threads: threads are created state, ready state, running state, blocking state, and death state.
Creates a state in which the object that produces the thread has not yet called the Start method before the state. When the Start method is called, it enters the ready state and waits for the JVM to dispatch. Once the JVM has dispatched the thread, the thread enters the running state. If the running thread is paused, it enters a blocking state. If the thread's Run method finishes executing or the thread calls the Stop method, the thread dies.
The priority of the thread. The highest level is 10, the minimum is 1, and the default is 5. The priority of the thread is set, and it is not necessarily high priority to execute first, but the probability of first execution is large. So the priority is high, the thread executes this sentence first is wrong.
Use the SetPriority () and GetPriority () methods to set and get the priority of the thread, respectively.
In general, you do not need to manually set the priority of a thread, but instead use the default priority.
This article is from the "JDK7 Environment Building" blog, please make sure to keep this source http://zzhhz.blog.51cto.com/7107920/1626187
Java Threading Summary