1. The Code executed by each thread is the same
If the code executed by each thread is the same, it is easier to share data. You can use the same Runnable object, which contains the shared data.
Public class multithread1_data1
{
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 = 20;
Public void run ()
{
While (allTicketCount> 0)
{
Sale ();
}
}
PublicSynchronizedVoid sale ()
{
System. out. println ("remaining" + allTicketCount );
AllTicketCount --;
}
}
The saleticketcount object contains the allTicketCount data to be shared. The two threads use the same SaleTickets to share allTicketCount.
2. The Code executed by each thread is different
Method 1: encapsulate the data to be shared into an object and pass the object to the Runnable object that executes different codes.
Method 2: Use the Runnable objects that execute different codes as internal classes.
See example: there are 4 threads, of which 2 threads each on j + 1, there are 2 threads each on the J-1. Addition and subtraction operations are unordered.
Method 1:
Public class multithread1_data3
{
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 ("add Thread", nInfo). start ();
New Thread (new NumberInfoMinus ("Thread Reduction", 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 ();
}
}
}
// Subtraction
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 multithread1_data4
{
Int j = 10;
Public static void main (String [] args)
{
Multithread1_data4 m = new multithread1_data4 ();
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 value:" + (++ j ));
}
Public synchronized void minus ()
{
System. out. println ("less than the limit value:" + (-- j ));
}
// Add
Class NumberInfoAdd implements Runnable
{
Public void run ()
{
Add ();
}
}
// Subtract
Class NumberInfoMinus implements Runnable
{
Public void run ()
{
Minus ();
}
}
}
The execution result may be:
Add thread -- Value: 11
Add thread -- Value: 12
Thread reduction -- Value: 11
Thread reduction -- Value: 10
The execution result may also be:
Add thread -- Value: 11
Thread reduction -- Value: 10
Thread reduction -- Value: 9
Add thread -- Value: 10
In fact, you can follow these methods to execute the same code in the thread. See Method 1:
Public class multithread1_data2
{
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 ();
}
}
}
For some code, see the thread video source code of instructor Zhang Xiaoxiang.
This article from the "IT Xu fat column" blog, please be sure to keep this source http://woshixy.blog.51cto.com/5637578/1357027