Java5 before, as in the following two kinds:
The first type:
New Thread () {}.start (); This means that the run method of the thread subclass object is called , and new Thread () {} represents an instance object of an anonymous subclass of thread, and the subclass plus the run The code after the method is as follows:
1 New Thread () {2 Public void run () {3 4 }.start ();
The second type:
new Thread (New Runnable () {}). Start (); This means calling thread object accepted Runnable object run method, new Runnable ( {}runnable An instance object of the anonymous subclass ,runnable run
1 New Thread (new Runnable () {2public void run () {3 4} 5). Start ();
starting with Java5 , there are a number of thread pools that create multithreading:
1Executorservice pool = Executors.newfixedthreadpool (3)2 for(inti=0;i<10;i++){3Pool.execute (Newrunable () {4 Public voidrun () {5 }6 });7 }8Executors.newcachedthreadpool (). Execute (Newrunable () {9 Public voidrun () {Ten } One }); AExecutors.newsinglethreadexecutor (). Execute (Newrunable ( -{ Public voidrun () { - } the});
There are two ways to implement it, usingNew Thread ()AndNew Thread (runnable)form, the first kind of direct call
ThreadOfRunmethod, so we tend to useThreadSubclass, which isNew Subthread ()。 The second kind of tune
UseRunnableOfRunMethod.
There are two ways to implement this, namely, inheritanceThreadClasses and implementationsRunnableInterface
UseSynchronizedKeyword Retouching synchronization method
Against the use ofstop () in an incoherent state, then other threads can examine and modify them in that state. The result is difficult to check out the
real problem. suspend () method is prone to deadlock. When calling suspend () down, but still hold the lock acquired prior to this. At this point, no other thread can access the locked capital
Source unless it is hang thread resumed running. For any thread, if they want to restore the target thread, and if they try to use any of the locked resources with
, a deadlock is created. Therefore, you should not use suspend () in your own thread A flag is placed in the span class= "FONTSTYLE0" class that indicates whether the thread should be active or suspended. If the flag indicates that the line thread executes
is pending, use wait () notify ()
restart thread
&NBSP;
/span>
There are several ways to implement a thread in Java? What keyword modifies the synchronization method? Why is the Stop () and suspend () methods not recommended?