Java Multithreading (i):

Source: Internet
Author: User

1. Two ways to implement threading Traditionalthread

Inherit the Thread class to implement the implements runable interface

2. Timer/timertask Timer use (traditionaltimertest)

Title: Write a Timer Implementation output A string, the first interval of 2s, the second interval of 4s, the third interval 2s , alternating. Two implementations (using a timer, using two timer).

Note: The static variable cannot be defined in an anonymous inner class .

Better open-source framework learning:Quartz

3. Thread Mutex technology (synchronized) (traditionalthreadsynchronized )

Similar to bank transfer issues

Case: Start two lines Shuo letter print name loop output, use synchronized to implement mutual exclusion

Note: Accessing local variables in an inner class is final.

Deadlocks can occur if multiple synchronized are used.

The static method of locking the "door Bolt" is the class, the normal method of the "door Bolt" is this .

To achieve mutual exclusion, thesynchronized must use the same "door Bolt".

4. Thread Synchronization communication Technology (wait,notify)traditionalthreadcommunication

Automatic login cases, encryption and decryption of cookie implementations, benefits of high cohesion and low coupling.

Interview question: The child thread loops ten times, the main thread loops 5 Times, the two alternately runs twice.

Recommended wording:

Synchronized (obj) {

while (<condition does not hold>)

Obj.wait ();

...//Perform action appropriate to condition

}

The code about thread synchronization is written in a shared variable and is not used in a write thread.

5. Thread-Scoped shared variables (ThreadLocal)threadscopesharedata($)

Implementation:map<thread,object> The use of Get values from the Map to obtain data. The data that is obtained separately belongs to this thread. Ensures that objects that are manipulated in this thread do not interfere with other threads.

  1. How multiple threads access shared objects and data
  2. if each thread executes the same code You can use the same object Span style= "Font-family:times New Roman;" >, this runnable Span style= "font-family: the song Body;" The data in the object is the shared data For example ticketing system
  3. If each thread executes different code , you need to use different Runnable objects , There are two ways to implement these Runnable data sharing between Objects :

A) encapsulate the shared data in another object, and then pass the object one at a to each Runnable object. Each thread also assigns the method of the shared data to that object, which makes it easy to achieve mutual exclusion and communication for each operation that is performed on that data. ( incoming runnable)

b) Use these Runnable objects as Internal classes in a class, share the data as member variables in the outer class, and assign each thread to the external class how to manipulate the shared data. To implement mutual exclusion and communication of individual operations on shared data, call These methods of the external class as individual Runnable objects of inner classes . (Runnable go to the common method to fetch )

c) The first two combinations: to encapsulate the shared data in another object, and each thread assigns the method of operation of the shared data to that object to complete, the object as a member variable in the outer class or a local variable in the method, each thread's Runnable Object as a member inner class or local inner class in an external class.

Summary: Several pieces of code to synchronize mutual exclusion are best placed in separate methods, which are placed in the same class, which makes it easier to implement thread synchronization mutex and communication between them.

The most extreme and simple way is to define a public static variable in any class or interface , and that variable will be shared by all threads.

Summary of some of the code is not uploaded, please you can give the answer in the comments ha!

Java Multithreading (i):

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.