Java Threading Understanding 002

Source: Internet
Author: User

This chapter is primarily about learning a new way to create threads, familiar with thread-common methods, and thread synchronization.

  1. Create a new thread by implementing the Runnable interface.
    1  Packageinterthread_v001;2 3  Public classrunable_v001ImplementsRunnable4 5 {6     //Thread Body7 8      Public voidRun ()9 Ten     { One          for(inti=0; i<100; i++) A         { -  -System.out.println ("runnablethread-->" +i); the  -             //The thread sleeps, enters the ready state after hibernation, and starts to preempt the CPU -  -             if(i = = 50) +  -             { +                 Try{ AThread.Sleep (2000); atSystem.out.println ("Sleeping 2000ms"); -  -                 } -                 Catch(Exception e) { - System.out.println (e); -  in                 } -             } to             //let the thread resources-->CPU resources, but will immediately go to seize the CPU +             if(i = = 70) -             { the Thread.yield (); *SYSTEM.OUT.PRINTLN ("Yield"); $             }Panax Notoginseng         } -     } the}

    1. The interface implementation class is instantiated first in the main function, and the object is passed as a parameter to the thread t = new Thread ( object ); constructor.
1  Packageinterthread_v001;2  Public classrunable_test_v0013 4 {5 6      Public Static voidMain (String args[])7 8     {9 Ten         //instantiate the Runnable interface implementation class first One  Arunable_v001 RV =Newrunable_v001 (); -  -         //instantiate the object directly with thread and pass the Runnable interface implementation class object as a parameter the  -Thread T =NewThread (RV); -  -         //Start Thread +  - T.start (); +  A         //set thread to maximum level at  - t.setpriority (thread.min_priority); -  -         //gets the level of the current thread -  -System.out.println ("Current thread level--" +t.getpriority ()); in  -         //execution methods in the main thread to  +          for(inti=0; i<100; i++) -  the         { *System.out.println ("mainthread-->" +i); $         }Panax Notoginseng     } -}

Main methods of threading

Thread synchronization

When there are multiple threads, the same thread body is called, and it is possible that an error occurs.

How to control the thread without error, use a thread synchronization method-synchronous code block synchronized(this) {} , using this method to ensure that two thread objects call Unified line Cheng, It is not called by the second thread until the thread body has finished executing.

Thread implementation class:

1  Packageinterthread_v001;2 3  Public classsync_v001ImplementsRunnable4 5 {6     //Implement thread Body7 8      Public voidRun ()9 Ten     { One         inti = 100; A  -         //Thread code block -  the          while(true) -  -         { -             //Synchronizing code blocks +             synchronized( This) -             { +  A                 //gets the name of the current thread at  -System.out.println (Thread.CurrentThread (). GetName () + "--" +i); -  -                 //I self-reduction -  -i--; in  -                 //make the thread Occupy resources to  + Thread.yield (); -  the                 //exiting while Loop *  $                 if(I < 0)Panax Notoginseng  -                 { the                      Break; +                 } A             } the         } +     } -}

Test class:

1  Packageinterthread_v001;2 3  Public classsync_testv0014 5 {6 7      Public Static voidMain (String args[])8 9     {Ten  One         //interface Implementation Class instantiation object A  -sync_v001 SThread1 =Newsync_v001 (); -  thesync_v001 sThread2 =Newsync_v001 (); -  -         -  +         //Instantiating Threads -  +Thread T1 =NewThread (sThread1); A  atThread t2 =NewThread (sThread2); -  -T1.setname ("Thread 1"); -  -T2.setname ("Thread 2"); -  in         //Start Thread -  to T1.start (); +  - T2.start (); the  *     } $ Panax Notoginseng}

Java Threading Understanding 002

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.