Common examples of Java multithreading __java

Source: Internet
Author: User
Tags ticket
Common examples of Java multithreading

Author: narcisoforher

Reprint: Click to read the full text view

I. Related knowledge:

Java multithreaded programming to the knowledge:

(i) operation of the same quantity

(ii) operation of the same object

(iii) The callback method uses

(iv) Thread synchronization, deadlock problem

(v) Thread communication

Wait a minute

Two Example one: Three ticket windows sell 20 tickets at the same time;

Program analysis: 1. Vote number to use the same static value

2. To ensure that there will not be sold the same number of votes, to Java multithreaded synchronization lock.

Design idea: 1. Create a platform class station, inherit thread, rewrite the Run method, and perform ticketing operations in the Run method. Ticket sales to use the Sync Lock: There is a platform to sell this ticket, the other platform to wait for this ticket sold out.

2. Create the Main method invocation class

(a) Create a platform class, inherit thread

Package com.xykj.threadStation;

public class Station extends Thread {

To assign a value to a thread name by constructing a method
Public Station (String name) {
Super (name);//Assign value to thread name

In order to keep the number of votes consistent, the number of votes to be static
static int tick = 20;

Create a static key
static Object ob = "AA";//value is arbitrary

Rewrite the Run method to achieve the ticket operation
public void Run () {
while (tick > 0) {
Synchronized (OB) {//This is important, you must use a lock,
The man who goes in will hold the key in his hand and come out to get the key.
if (Tick > 0) {
System.out.println (GetName () + "sold the first" + Tick + "ticket");
} else {
System.out.println ("tickets sold out");
try {
Sleep (1000);//Rest One second
catch (Interruptedexception e) {
E.printstacktrace ();



(ii) Creating the Main method invocation class

Package com.xykj.threadStation;

public class MainClass {
* Use of Java multithreaded synchronous locks
* Example: three ticket window selling 10 tickets at the same time
* */
public static void Main (string[] args) {
Instantiate the platform object and name each platform
Station Station1=new Station ("Window 1");
Station Station2=new Station ("Window 2");
Station Station3=new Station ("Window 3");

Let each platform object start its own work
Station1.start ();
Station2.start ();
Station3.start ();



Program Run Result:

You can see the number of votes is not wrong.

Three Example two: Two people ab through an account a at the counter to take money and B to withdraw money at the ATM machine.

Program Analysis: The amount of money to be set into a static variable. Two people to take the same object value

(a) Create a bank class

public class bank {
//Suppose an account has 1000 dollars
Static int money =  1000;
//Counter Counter the way to withdraw the money
Public void counter (Int money)  {//parameter is the money taken each time money;
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: 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.