Sharing data between threads

Source: Internet
Author: User

one, each thread executes the same Code

If each thread executes the same code, it is more convenient to share the data. You can use the same Runnable object, which has the shared data in the Runnable object.

public class MultiThreadShareData1
{
public static void Main (string[] args)
{
Saletickets sale = new Saletickets ();
New Thread (Sale). Start ();
New Thread (Sale). Start ();
}
}

Class Saletickets implements Runnable
{
public int allticketcount =;

public void Run ()
{
while (Allticketcount > 0)
{
Sale ();
}
}

Public synchronized void Sale ()
{
System.out.println ("left" + allticketcount);
allticketcount--;
}
}
Saletickets This object has to share the data allticketcount, two threads use the same saletickets, can share allticketcount.


Two, each thread executes the code to be different
Method 1: Encapsulate the data that needs to be shared into an object and pass it to the Runnable object that executes the different code.
Method 2: Use the Runnable object that executes different code as an inner class.
See example: There are 4 threads, of which there are 2 threads on each pair of j+1, and 2 threads on each pair of j-1. The addition and subtraction operation has no order.

Method 1:
public class MultiThreadShareData3
{
public static void Main (string[] args)
{
int j = 10;
Numberinfo ninfo = new Numberinfo (j);
for (int i = 0; i < 2; i++)
{
New Thread (new Numberinfoadd, Ninfo). Start ();
New Thread (New Numberinfominus ("Subtract Threads", Ninfo)). Start ();
}
}
}

Class Numberinfo
{
private int number;

public numberinfo (int number)
{
This.number = number;
}

public int GetNumber ()
{
return number;
}

public void Setnumber (int number)
{
This.number = number;
}

public void Add ()
{
System.out.println ("Value:" + (++number));
}

public void Minus ()
{
System.out.println ("Value:" + (--number));
}
}

Add operation
Class Numberinfoadd implements Runnable
{
private String name;
Private Numberinfo Ninfo;

Public Numberinfoadd (String name, Numberinfo ninfo)
{
THIS.name = name;
This.ninfo = Ninfo;
}

public void Run ()
{
Add ();
}

public void Add ()
{
synchronized (ninfo)
{
System.out.print (name + "--");
Ninfo.add ();
}
}
}

Minus operation
Class Numberinfominus implements Runnable
{
private String name;
Private Numberinfo Ninfo;

Public Numberinfominus (String name, Numberinfo ninfo)
{
THIS.name = name;
This.ninfo = Ninfo;
}

public void Run ()
{
Minus ();
}

public void Minus ()
{
synchronized (ninfo)
{
System.out.print (name + "--");
Ninfo.minus ();
}
}
}


Method 2:
public class MultiThreadShareData4
{
int j = 10;
public static void Main (string[] args)
{
MULTITHREADSHAREDATA4 m = new MultiThreadShareData4 ();
for (int i = 0; i < 2; i++)
{
New Thread (M.new Numberinfoadd ()). Start ();
New Thread (M.new Numberinfominus ()). Start ();
}
}

Public synchronized void Add ()
{
System.out.println ("added after value:" + (++J));
}

public synchronized void minus ()
{
System.out.println ("Minus a few values:" + (--J));
}

Increase
Class Numberinfoadd implements Runnable
{
public void Run ()
{
Add ();
}
}

Reducing
Class Numberinfominus implements Runnable
{
public void Run ()
{
Minus ();
}
}
}

The results of the execution may be:

Thread Increase--number: 11
Thread Increase--number: 12
Minus thread--number: 11
Minus thread--number: 10

The results of the execution may also be:
Thread Increase--number: 11
Minus thread--number: 10
Minus thread--Number: 9
Thread Increase--number: 10

In fact, threads executing the same code can also do so by looking at a Method 1:

public class MultiThreadShareData2
{
public static void Main (string[] args)
{
Ticketinfo tinfo = new Ticketinfo (20);
New Thread (New SaleTickets2 ("Thread 1", Tinfo)). Start ();
New Thread (New SaleTickets2 ("Thread 2", Tinfo)). Start ();
}
}

Class Ticketinfo
{
private int allticketcount;

Public ticketinfo (int allticketcount)
{
This.allticketcount = Allticketcount;
}

public int Getallticketcount ()
{
return allticketcount;
}

public void Setallticketcount (int allticketcount)
{
This.allticketcount = Allticketcount;
}

public void Sale ()
{
System.out.println ("Remaining:" + allticketcount--);
}
}

Class SaleTickets2 implements Runnable
{
private String name;
Private Ticketinfo Tinfo;

Public SaleTickets2 (String name, Ticketinfo tinfo)
{
THIS.name = name;
This.tinfo = Tinfo;
}

public void Run ()
{
while (Tinfo.getallticketcount () > 0)
{
Sale ();
}
}

public void Sale ()
{
Synchronized (Tinfo)
{
System.out.print (name + "--");
Tinfo.sale ();
}
}
}

Part of the code reference Zhang Xiaoxiang: teacher thread video source.

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.