Java High Salary Road __006_ multithreading

Source: Internet
Author: User

There are two ways to create a thread

1  Packagelearn. Javabasics.class;2 3  Public classThreaddemoextendsThread {4     Private Static intCount = 1;5     6      Public voidrun () {7System.out.println ("Thread:" +count+ "has started");8++count;9     }Ten  One     /** A      * @paramargs -      */ -      Public Static voidMain (string[] args) { the         //TODO auto-generated Method Stub -Threaddemo T1 =NewThreaddemo (); -Threaddemo t2 =NewThreaddemo (); -Threaddemo t3 =NewThreaddemo (); +                  -         NewThread (NewRunnable () { +  A @Override at              Public voidrun () { -                 //TODO auto-generated Method Stub -System.out.println ("Runnable Thread has started"); -             } -              - }). Start (); in          - T1.start (); to T2.start (); + T3.start (); -     } the}
View Code

Thread synchronization

Because a thread is involved in the operation of shared data while the other thread is not completed, there is a security problem with the shared data
How to resolve:
1. Synchronizing code blocks
Synchronized (Sync monitor) {
Manipulating code that shares data
}
Synchronization monitor, which is the object of any one class to act as, which thread obtains the monitor, which thread can manipulate the shared data, commonly known as: lock
All threads must have the same synchronization monitor, that is, the object of this class can only have one (the form of an interface using this is generally not in the way, the inheritance may exist multiple objects, use this)
2. Synchronization method
Can be similar to the understanding that the synchronization monitor at this time is this function

Thread Communication

1  Packagelearn. Javabasics.class;2 3 classPrintnumImplementsrunnable{4     Private intnum = 1;5     6 @Override7      Public voidrun () {8         //TODO auto-generated Method Stub9          while(Num <= 100) {Ten             synchronized( This) { OneNotify ();//Wake up a top-of-the-wait thread A                 Try { -Thread.CurrentThread (). Sleep (10); -}Catch(interruptedexception e) { the                     //TODO auto-generated Catch block - e.printstacktrace (); -                 } -System.out.println (Thread.CurrentThread (). GetName () + ":" +num); +++num; -                  +                 Try { AWait ();//wait for this thread to drop at}Catch(interruptedexception e) { -                     //TODO auto-generated Catch block - e.printstacktrace (); -                 } -             } -         } in     }     - } to  +  Public classtestcommunication { -     /** the * Alternate printing of values from 1 to 100 *      */ $      Public Static voidMain (string[] args) {Panax Notoginseng         //TODO auto-generated Method Stub -Printnum p =Newprintnum (); the          +Thread T1 =NewThread (p); AThread t2 =NewThread (p); the          +T1.setname ("A"); -T2.setname ("B"); $          $ T1.start (); - T2.start (); -     } the}
View Code

Java High Salary Road __006_ multithreading

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.