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?

Source: Internet
Author: User

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.