Java supports multithreading. You can define the Thread class by continuing the Thread class or implementing the Runnable interface, and rewrite the run method in this class.
A simple Java Thread class MyThread is shown below.
Package com. wt. testThread;
Public class MyThread extends Thread {
Public void run ()
{
Int I = 0;
While (true)
{
I ++;
System. out. println ("MyThread Processing -->" + I );
Try {
Thread. sleep (500 );
} Catch (InterruptedException e ){}
}
}
Public static void main (String args [])
{
MyThread thread = new MyThread ();
Thread. start ();
}
}
In the run method of MyThread, information is output once every 500 ms (Thread. sleep (500. In the main function, create a MyThread Instance Object and start the thread instance using the start method of the object. The program output result is as follows.
MyThread Processing --> 1
MyThread Processing --> 2
MyThread Processing --> 3
......
Multi-thread synchronization
Suppose there are now two tools, three workers, each of which needs to be used at the same time. Use Java multithreading to simulate the above scenario, use the Thread class WorkThread to represent the worker, and use ToolBox to represent the ToolBox where the two tools are located, as shown below.
Class ToolBox {
// T1Owner and t2Owner indicate the worker names that are using the tool respectively
Private String t1Owner;
Private String t2Owner;
// UseTool indicates that a worker obtains the tool and uses it
Public void useTool (String workerName) throws InterruptedException
{
T1Owner = workerName;
Thread. sleep (100 );
T2Owner = workerName;
Thread. sleep (100 );
CheckTool ();
}
// UseTool check whether the tool is being used by the same worker
Public void checkTool ()
{
If (! T1Owner. equals (t2Owner )){
System. out. println ("* Wrong *: Tool1:" + t1Owner + "\ t Tool2:" + t2Owner );
}
Else {
System. out. println ("* Right *: Tool1:" + t1Owner + "\ t Tool2:" + t2Owner );
}
}
}
Public class WorkerThread extends Thread {
Private ToolBox toolBox;
Private String workerName;
Public WorkerThread (String workerName, ToolBox toolBox)
{
Super ();
This. workerName = workerName;
This. toolBox = toolBox;
}
Public void run ()
{
// The worker takes one second off each time and then uses the tool to work
While (true)
{
Try {
Thread. sleep (1000 );
ToolBox. useTool (workerName );
} Catch (InterruptedException e ){}
}
}
}
The main function is as follows. The ToolBox class instance toolBox represents the toolBox shared by workers. Three Worker thread classes are constructed through the constructor of the WorkerThread class, and the worker name and ToolBox are input to start the thread.
Public static void main (String args [])
{
// Toolbox shared by workers
ToolBox toolBox = new ToolBox ();
// Three workers start to work
New WorkerThread ("Tom", toolBox). start ();
New WorkerThread ("Jack", toolBox). start ();
New WorkerThread ("Bob", toolBox). start ();
}
The execution result is as follows.
* Right *: Tool1: Bob Tool2: Bob
* Right *: Tool1: Bob Tool2: Bob
* Wrong *: Tool1: Bob Tool2: Jack
* Wrong *: Tool1: Bob Tool2: Jack
* Wrong *: Tool1: Bob Tool2: Jack
* Right *: Tool1: Jack Tool2: Jack
* Right *: Tool1: Jack Tool2: Jack
* Right *: Tool1: Jack Tool2: Jack
* Wrong *: Tool1: Jack Tool2: Tom
* Wrong *: Tool1: Jack Tool2: Tom
* Wrong *: Tool1: Jack Tool2: Tom
* Right *: Tool1: Tom Tool2: Tom
It can be seen that the three worker thread classes share the tools in the toolbox, resulting in competition for tool usage. Because the code of the useTool method in ToolBox (that is, the tool used by workers) has resource competition between the preceding threads, it is called the Critical Section ). The critical section is as follows.
T1Owner = workerName;
Thread. sleep (100 );
T2Owner = workerName;
Thread. sleep (100 );
CheckTool ();
For the code in the preceding critical section, you need to implement synchronization, that is, either all or none of the code is executed in a thread. In Java, you can use the keyword synchronized for synchronization. Synchronized can be used in two ways:
1) for shared instance objects to be synchronized, add synchronized as the modifier of the method, as shown below.
Public synchronized void useTool (String workerName) throws InterruptedException
2) Add the code of the critical section to be synchronized to synchronized (this) {}, as shown below.
Synchronized (this)
{
T1Owner = workerName;
Thread. sleep (100 );
T2Owner = workerName;
Thread. sleep (100 );
CheckTool ();
} Www.2cto.com
After synchronized is used for synchronization, run the original primary function again to obtain the correct execution result.
* Right *: Tool1: Tom Tool2: Tom
* Right *: Tool1: Bob Tool2: Bob
* Right *: Tool1: Jack Tool2: Jack
* Right *: Tool1: Tom Tool2: Tom
* Right *: Tool1: Bob Tool2: Bob
* Right *: Tool1: Jack Tool2: Jack
* Right *: Tool1: Tom Tool2: Tom
* Right *: Tool1: Bob Tool2: Bob
* Right *: Tool1: Jack Tool2: Jack
* Right *: Tool1: Tom Tool2: Tom
* Right *: Tool1: Bob Tool2: Bob
* Right *: Tool1: Jack Tool2: Jack
* Right *: Tool1: Tom Tool2: Tom
* Right *: Tool1: Bob Tool2: Bob
* Right *: Tool1: Jack Tool2: Jack
* Right *: Tool1: Tom Tool2: Tom
Author: zephiruswt