Java Multithreading programming attention problems

Source: Internet
Author: User
Tags ticket
Java Multithreading programming attention problems

Multi-threaded concept ask Niang, most of them are stereotyped. Programmers who are not very rich in programming experience are more difficult to digest.
The author analyzes the problems that should be paid attention to in Java multithreading programming from the angle of practical application.
Application Scenario Assumptions:
A cinema has three ticket outlets,
Used to sell tickets to children, adults and the elderly respectively. The movie theater has 100 tickets for each window,
They are children's tickets, adult's and old-age tickets respectively. (a conductor is equivalent to a CPU, the cinema can provide up to 3 conductors).
Question: How to make the movie theater ticket speed fastest. (Many people are waiting in line for tickets).
So imagine, we are open 3 ticket window, let 3 conductor all move up. This is multithreading.

Programmers in the Java platform multithreaded programming know that thread programming has 2 implementations that inherit thread and implement the Runnable interface.
Let's look at the inheritance method first.
public class Mutlithread extends thread{
private int ticket=100;//Each thread has 100 tickets
public void Run () {
while (ticket>0) {

System.out.println (ticket--+ "is saled by" +thread.currentthread (). GetName ());
try {
Thread.Sleep (2000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}
Test class
public class Mutlithreaddemo {
public static void Main (String [] args) {
Mutlithread m1=new mutlithread ("Window 1");
Mutlithread m2=new mutlithread ("Window 2");
Mutlithread m3=new mutlithread ("Window 3");
M1.start ();
M2.start ();
M3.start ();
}
}

Question 2. If the cinema total is 100 tickets, 3 of the conductor's colleagues ticket. Whether the above method of inheritance can satisfy the requirement.
After analysis obviously cannot satisfy, because cannot realize the multithread resource sharing. This time we need to implement the Runnable interface approach.

public class Mutlithread implements runnable{
private int ticket=100;//Each thread has 100 tickets
public void Run () {
while (ticket>0) {

System.out.println (ticket--+ "is saled by" +thread.currentthread (). GetName ());
try {
Thread.Sleep (2000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}

Test class:

public class Mutlithreaddemo {
public static void Main (String [] args) {
Mutlithread m=new Mutlithread ();
Thread T1=new thread (M, "window" +i);
Thread T2=new thread (M, "window" +i);
Thread T3=new thread (M, "window" +i);
T1.start ();
T2.start ();
T3.start ();
}
}


For the 2nd multi-thread, our thread is not secure and is executed 100 times. The answer is yes.
The author makes a comparison of the printed statements. It was 100 times indeed.
If you change the variable to static, it is greater than 100 times. is not to meet our business needs.

In fact, writing a multithreaded class is not difficult, it is difficult to analyze our thread is not safe. This security is to meet our business needs.

That in multithreaded programming, we should pay attention to what the problem.
Summarize:
1. Multithreading sharing resources, performing the same task, requires runnable interface.
2 static methods are usually safe. Static member variables are not secure.
3. How to solve our security problems, the usual approach is to lock the mechanism, or with ordinary member variables.

4. Examples: HashMap and Currenthashmap. Difference: HashMap is more efficient than currenthashmap execution. But the thread is not secure. And the Currenthashmap thread is safe.

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.