Java multithreading (Overview)

Source: Internet
Author: User

1. multithreading Definition

If a program has multiple execution paths, it is called multithreading.

Process: The program being executed.

Thread: The execution path of the program. Execution Unit.

Single thread: if an application has only one execution path, it is called a single thread.

Multithreading: a program with multiple execution paths is called multithreading.

2. Two solutions

1,Inherit Thread class

1. The custom class inherits the Thread class.

2. Rewrite the run () method.

3. The write test class is used to create a custom class object.

4. Call the start () method.

The first method is MyThread thread {@ override run () {(I = 0; I <100; I ++) {System. out. println (getName () + "" + x) ;}} Test {main (String [] args) {MyThread m1 = MyThread (); MyThread m2 = MyThread (); m1.setName (""); m2.setName (""); m1.start (); m2.start ();}}

2,Implement the Runnable interface

1. Customize the class to implement the Runnable interface

2. Rewrite the run () method.

3. Create a custom class object for the write test class

4. Call the start () method.

Method 2: MyRunnable Runnable {@ override run () {(I = 0; I <100; I ++) {System. out. pritnln (Thread. currentThread (). getName () + "" + I) ;}} Test {main (String [] args) {MyRunnable m = MyRunnable (); Thread t1 = Thread (m, ""); Thread t2 = Thread (m, ""); t1.start (); t2.start ();}}

Note: Since the first implementation scheme is available, why is the second scheme required?

Because the class can only be inherited, and the interface can be implemented in multiple ways.

 

3, Lock

1. New Features of JDK 5: it defines when to obtain the lock and when to release the lock.

2. deadlock:

In synchronization, multiple threads use multiple locks to wait for each other

3. Inter-thread Communication

Different types of threads operate on shared data

4. Methods to be mastered:

Priority

Public final void setPriority (int newPriority)
Public final int getPriority ()

The default thread priority is 5.

The thread priority ranges from 1 to 10.

Note: The thread priority. Generally, the effect is not great.

In case of a large number of times, objects with a higher thread priority may obtain the execution right first than those with a higher thread priority.

Join

Public final void join ()

Wait for the thread to terminate ,.

Note: adding a thread must be an action performed after the thread is enabled.

Pause

Public static void yield ()

Pause the currently executed thread object when other threads are executed.

It can only ensure the harmonious operation of the program to a certain extent. If you want to achieve the effect, you need to use the wait wake-up mechanism.

Daemon thread

Public final void setDaemon (boolean on)

 

4. Notes

1. How does thread security problem occur? How can this problem be solved?

First judgment:

How is it generated?

A. Is the program multithreading?

B. Is there any shared data?

C. Whether there are multiple statements to operate on shared data

Solution:

Manage Step c.

1. Synchronize code blocks

Format:

Synchronized (object ){

Code to be locked

}

Note: The object can be any object, but the synchronized object must be consistent.

2. Synchronization Method

Add synchronized to the method declaration..

Note: this object is synchronized.

2,Who is the lock of the static method?

The object of the bytecode file of the current class.

3,What is the difference between run () and start?

Run () is just a normal call

Start () does two things: start the thread and call run ()

4,What is the difference between sleep () and wait?

Sleep (): Indicates sleep. You must specify the sleep time without releasing the lock.

Wait (): Indicates waiting. You can release the lock without specifying the sleep time.

5. Case studies

Ticket Selling case ticket Runnable {Ticket = 100; x = 0; @ override run () {(x % 2 = 0) {(Ticket .) {(ticket> 0) {System. out. println (Thread. currentThread (). getNameI () + "" + (ticket --) ;}}{}} sendTicket () {(ticket> 0) {System. out. println (Thread. currentThread (). getName () + "" + (ticket --) ;}} test {main (String [] args) {Ticket t = Ticket (); Thread t1 = Thread (t, ""); Thread t2 = Thread (t, ""); Thread t3 = Thread (t, ""); t1.start (); t2.start (); t3.start ();}}

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.