Many Java implementations at the language level
Thread Some operation primitives are easy to use, unlike calling operations such as C and C ++.
System Function To implement multithreading. Here is an example to show how Java multithreading is simple and mysterious. Java makes it easy for beginners to handle multiple tasks and threads that are often described in textbooks.
Code . (But it is also very convenient to create deadlock, it is difficult to find a deadlock, if you do not understand it, it is best not to use, not necessary occasions do not use ). As for the cloud in the book, some thread states and the transformation between states are the same. Java has one more thread group to facilitate the group method.
Management A large number of threads. For more information, see the API or Google or Baidu. record it for reference. If you can write the producer consumer (also called the reader writer) by yourself, it will be 3 or 5 successful, in this way, you can consider using it in your production code.
The main thread class is a thread class. The Thread class has a run () method and a thread. Start () method to start the thread. There are two methods to implement multithreading:
1. encapsulate the operations to be executed in parallel into a class inherited from thread
Code
Public Class Thread_ex Extends Thread ...{
Public VoidInit ()...{
}
Public VoidStart ()...{
//** Will CLL run () defualt.
}
Public VoidStop ()...{
}
Public VoidRun ()...{
//** Do some parallequalation works.
}
}
2. Extend the runnable interface to meet the requirements for multi-threading that inherits other classes (Because Java does not support multi-Inheritance like C ++)
Public Class Threadtest Extends Fatherclass Implements Runnable
Public Void Init ()......{
}
Public VoidStart ()......{
//** Will CLL run () defualt.
}
Public VoidStop ()......{
}
Public VoidRun ()......{
//** Do some parallequalation works.
}
}
Several threadsKeyWords are very important:Synchronize waitNotIfy policyall
If they cannot understand them well, they should not be used. A brief introduction is as follows:
SynchronizeModifier that defines an object, method, or attributeAccessSequential access is required. As the name implies, a clerk is a synchronize object just like a bank is waiting to get money. Only those who have no money can wait in the queue, and those VIPs will not be needed.
Wait and Policy policyallYes, they are object-level locks, and Yu native is blunt. This is only understandable after many materials are turned over. If they cannot be understood, let's look at the code. One of the multiple threads accesses shared resources and finds someone using it, so it is wait on this resource. If the user is used up, notify y () to notify the next waiting person to come in. notifyall () that is to say, let's wait together! HoweverServiceAnd a person, so everyone rushed in, the strength of the natural advantage. But in the Java World, the JVM has the final say.
Synchronize must exist in places with wait and notify. In turn, it is not true. Wait and notify are not in the force of thread.
-----------------------------------------------------------
Let's get straight to the code! In this example, three classes are designed: threadcontroller, printthread, and resource. Put the Code:
Code
Public Class Resource ...{
Public Static Boolean Inuse;
// Public Boolean inuse = false;
String name;
Int Sleeptime = 0 ;
Public Synchronized Void Out (string N )...{
This . Name = N;
While (Resource. inuse = True )
Try ...{
System. Out. println ( " Name: " + Getname () + " Waiting... " );
This . Wait ();
} Catch (Interruptedexception IE )...{
Ie. printstacktrace ();
}
Resource. inuse = True ; // Entry the block and lock it
Try ...{
Sleeptime = ( Int ) (Math. Random () * 5000 );
System. Out. println ( " Name: " + Getname () + " In use; Define sleep time: " + Sleeptime + " , Wait 50000 to exit the synchronize Block " );
Thread. Sleep ( 5000 );
System. Out. println ( " Name: " + Getname () + " End 5000 sleep. Sleep some time after running y to test if others can end first " );
Thread. Sleep ( 5000 - Sleeptime );
System. Out. println ( " Name: " + Getname () + " Will exit the synchronize Block " );
Resource. inuse = False ;
System. Out. println ( " Name: " + Getname () + " End. " + Resource. inuse );
This . Policyall ();
} Catch (Interruptedexception IE )...{
Ie. printstacktrace ();
}
}
Public String getname ()...{
Return Name;
}
}
Code
Public Class Threadcontroller ...{
Public Static Void Main (string ARGs [])... {
Printthread T1, T2, T3, T4;
Resource R = New Resource ();
Resource. inuse = False ;
T1 = New Printthread ( " 1 " , R );
T2 = New Printthread ( " 2 " , R );
T3 = New Printthread ( " 3 " , R );
T4 = New Printthread ( " 4 " , R );
T1.start ();
T2.start ();
T3.start ();
T4.start ();
System. Out. println ( " Controller ended... " );
// System. Exit (0 );
}
}
Code
Public Class Printthread Extends Thread ...{
Int Sleeptime;
Resource RS;
Public Printthread (string ID, resource R )...{
Super (ID );
This . RS = R;
}
Public Void Run ()...{
Try ...{
System. Out. println ( " Thread: " + Getname () + " Start ...... wait to entry synchronize Block " );
Rs. Out (getname ());
Sleeptime = ( Int ) (Math. Random () * 5000 );
System. Out. println ( " Thread: " + Getname () + " Start to sleep " + Sleeptime + " MMS " );
Sleep (sleeptime );
} Catch (Interruptedexception IE )...{
Ie. printstacktrace ();
}
System. Out. println ( " Thread: " + Getname () + " Wakeup, end. " );
}
}