The process and thread process is a program execution sequence, in the multitasking operating system, by operating system scheduling and managed; thread is a subsequence in a process, and thread is managed by the program. In multi-threaded environments, threads share the same address space, communication between threads is very convenient, and they together constitute a process, while the process has different address space. Process and thread are dynamic concepts. The difference between the process and the program is that the process is a scheduling of the operating system when the program is executed; the program is static. When the program is executed, the scheduling of the operating system is obtained, and one process of the operating system is formed, and it is run according to the command sequence of the program. Threads can be managed by the program, you can create threads, start threads, suspend threads, termination threads, and clear the thread from the program. Java uses multi-threads to implement the entire execution environment as asynchronous. In the Java language, the thread is characterized by a thread class, and the thread class encapsulates all the required thread operation control. Thread objects and running threads Difference: You can view thread objects as a control panel that runs threads, there are many methods in thread objects to control whether a thread is run, sleep, hang, or stop. Thread class is the only means of controlling thread behavior. After a Java program is started, there is already a thread running, and can see the current running that the current run is the thread through the Thread.currentThread () method. You get a thread's handle, you can do a very interesting thing, even if the single thread is the same.
The thread's lifecycle Java thread is generated from the destroyed state: New State (NewBorn): The thread has been created (the thread object has been assigned a memory space, private data has been initialized) but has not been implemented. At this time, the thread can be scheduled, becomes an operational state, or can be killed and become a stopped state. Ready Status or Run ABLE: The thread is waiting for the CPU resource and can be scheduled at any time. At this time, the thread has been scheduled and has been placed in the ready queue. As for when the thread is executed, it depends on the priority of the thread and the current situation of the ready queue. Running: The thread is running, the thread already has the control of the CPU, letting the CPU control power: 1, thread running. 2. There is a thread that is higher than the current thread priority. 3, thread active sleep one end. 4, thread is waiting for a certain resource. Hanging Status (Block): The thread at this time cannot enter the ready queue. Threads in the suspended state typically need to be awakened by certain events, as for what event awaken the thread, depending on the reason for the hang. Stop State (DEAD): The thread has exited the running status and no longer enters the ready queue. The reason is that the thread has been executed, or it may be forced to kill another process.
The scheduling and priority scheduling of the thread: concurrent execution of multiple threads is actually scheduling through a scheduler. Scheduling means allocating CPU resources between each thread. Two models of thread scheduling - preemptive models and timed models. Java supports the preemptive model. Therefore, in order to make the low priority thread have the opportunity to run, the high priority thread should actively enter the "sleep" state from time to time. Priority: Mythread.SetPriority (i); i is 1 to 10 levels.
Thread synchronization and deadlock synchronization: Multiple threads simultaneously access (write) the same resource. Dead lock: too much in DB! I am not talking here. The creation of threads creates a thread to complete three things: establish a virtual CPU; give the execution code for the thread; provide data to the code. In Java, the virtual CPU is encapsulated in the Thread class. Creating a Thread object is equivalent to establishing a virtual CPU. The Java's object-oriented model requires the code and data provided by the thread to exist in an instance of the class. First: Creating a subclass of the Thread class override the run () method provides the execution code of the thread, defines the member variable of the Thread class to provide the thread data.
Package mythread;
Public class mythread extends thread {
INT mycount = 1, mynumber;
Public mythread (int Num) {
MyNumber = NUM;
System.out.println ("Create Thread" MyNumber);
}
Public void run () {
While (true) {
System.out.println ("Thread" MyNumber ": Mode" Mycount);
Mycount ;
While (mycount == 6)
Return;
}
}
Public static void main (String [] args) {
For (int i = 0; i <3; i ) {
New mythread (i 1) .start ();
}
}
} Second: Implementing the runnable interface This is a unilaterally used interface. Public interface java.lang.runnable {
Public Abstract void Run ();
} The instance of a class that implements the runnable interface is passed to the Thread class constructor, created an instance of a Thread class, this Thread class instance will use the Run () method implemented in its own class as its execution code. You can also use data in the instance of our defined classes as the operational data. Note that the Runnable interface does not have any support for threads (in fact Thread also implemented it), you must also create an instance of the Thread class. Public class twothread ustements runnable {
Thread mythread;
INT a; / / Define the data we have to operate
Public twothread (int a) {
THIS.A = a;
}
Public void start () {
mythread = new thread (this); // put your ship in the post method of THREAD START
Mythread.start (); // Start automatically calls RUN method
}
Public void run () {
/ / Write our processing code
}
}
Thread Synchronization Synchronized Java has a synchronous model - monitor, responsible for managing access to synchronous methods in the object, is: Give the object unique key, when multiple threads enter the object, only The thread of the object key can access the synchronization method, and other threads are waiting in the object until the thread uses the WAIT method to abandon this key, and other waiting threads will be executed after the thread of the key, and no key thread Still blocked in this object. Other one or all threads can usually be awakened with a notify () or notigyall () method. Thread communication pipe flow (PIPE) is used to transfer data directly between different threads. It has two types support for PiPEINPUTSTREAM and PIPEOUTPUTSTREAM.