Java --- 12 --- multi-thread exercise: Ticket Selling --- data sharing,

Source: Internet
Author: User

Java --- 12 --- multi-thread exercise: Ticket Selling --- data sharing,

Do an exercise: sell tickets to small programs to achieve data sharing.

 

Let's first create a thread and run it alternately with the main thread.

 

This is relatively simple:

Let's just inherit the Thread class:


class MyThread extends Thread{    private int num = 150;    private String name;    public MyThread(String name)    {        super(name);    }    public void run ()    {        while (num >= 0)        {            System.out.println(Thread.currentThread().getName()+"..."+num--);        }    }}public class THREAD{    public static void main (String[] args)    {        MyThread a = new MyThread("My");        MyThread b = new MyThread("Your");        a.start();        b.start();    }}


Through this example, we can find that the num value is repeatedly printed, which is inconsistent with the phenomenon in our real life, such as selling tickets.

When selling tickets at the railway station, there were multiple windows at the same time, but no repeated tickets were found.

Create four threads, which are equivalent to four windows to sell tickets:


class MyThread extends Thread{    private int num = 150;    private String name;    public MyThread(String name)    {        super(name);    }    public void run ()    {        while (num >= 0)        {            System.out.println(Thread.currentThread().getName()+"..."+num--);        }    }}public class THREAD{    public static void main (String[] args)    {        MyThread a = new MyThread("one");        MyThread b = new MyThread("two");        MyThread c = new MyThread("three");        MyThread d = new MyThread("four");        a.start();        b.start();        c.start();        d.start();    }}

However, the running results show that each ticket has been repeatedly sold for four times. If this happens in real life, there will still be no tickets to go home?

How to solve the problem.

 

Declare the num variable as static.


Define data as static for data sharing:



class MyThread extends Thread{    private static int num = 150;    private String name;    public MyThread(String name)    {        super(name);    }    public void run ()    {        while (num >= 0)        {            System.out.println(Thread.currentThread().getName()+"..."+num--);        }    }}public class THREAD{    public static void main (String[] args)    {        MyThread a = new MyThread("one");        MyThread b = new MyThread("two");        MyThread c = new MyThread("three");        MyThread d = new MyThread("four");        a.start();        b.start();        c.start();        d.start();    }}


The method of inheriting the Thread class has already achieved data sharing, but we generally do not define static because the static life cycle is too long.

How can we implement the Runnable interface?


We first use this method to create four threads for thread running.


class MyThread implements Runnable{    private static int num = 150;    public void run ()    {        while (num >= 0)        {            System.out.println(Thread.currentThread().getName()+"..."+num--);        }    }}public class THREAD{    public static void main (String[] args)    {        MyThread a = new MyThread();        Thread a1 = new Thread(a);        Thread a2 = new Thread(a);        Thread a3 = new Thread(a);        Thread a4 = new Thread(a);        a1.start();        a2.start();        a3.start();        a4.start();    }}


We found that the ticket sales program has been implemented and data sharing has been implemented. What's going on?

Why is it possible to implement the Runnable interface?

Let's take a look at this question:

Why should we pass the subclass object of the Runnable interface as an actual parameter to the constructor of the Thread class?

Because the custom run method belongs to a subclass object of the Runnable interface,

The run method must be run after the Thread starts, but the new Thread object does not re-write the run method.

To run a thread, you must have a run method, but you do not have a run method. Therefore, you must specify the object to which the run method belongs. It is equivalent to the run method of the borrowed Runnable subclass object.


So far, the two ticket selling methods have been implemented.



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.