Multi-thread programming in Java (j2se entry 14)

Source: Internet
Author: User

Java multi-thread programming

Here we need to recall the process, that is, the running program, a concurrent task in the multitasking operating system (The CPU runs multiple processes on time slice.), Thread. Its essence is the sequential execution process in the process,The process has an independent process space. The data storage space in the process (for space and stack space) is independent.. Threads do not have independent data storage space. Their data storage space (heap space) is shared, the stack space between threads is independent, and the resources consumed by threads are smaller than those consumed by processes.

A thread is a sequential execution process in a process (a running program ).The process can be divided into multiple threads.

JVM (Java Virtual Machine) is a process. Java can only apply to create threads..

The operating system determines whether a thread has a priority. In an exclusive operating system, the system has a priority concept. A shared operating system does not have a priority.

A thread in Java is also an object, a thread class is a thread class, And a thread is a resource created and maintained by the operating system. A thread object can only represent one thread, but it is not a thread. You can only create and abort a thread through a thread object to access the underlying thread resources.

The thread contains three parts: CPU resources, code, and data storage space.

When the thread object calls the START () method, the thread object will apply to the operating system for thread startup. In addition to the applied thread, there is also the main method thread, that is, the main thread.

Note: Only running threads have the opportunity to execute code. The suspension of the main thread does not affect other running threads. The main thread is terminated, that is, the main () method exits, only when all threads in a process are aborted will the process (JVM process) Exit. As long as there is a thread that has not been aborted, the process will not exit.

Two Methods of Thread Programming
1. Write a class that inherits the Thread class and overwrites the run () method inherited from the Thread class. In this way, the custom Thread class is well written.
2. Write a class, implement the runable interface, and implement the run () method. The object of the class written in this method must be used as the parameter of the constructor when the Thread class creates an object.

Implementation Form 1:
Thread t = new ();
Class A extends thread {
Public void run (){
//...
}
}
Implementation Form 2:
Runable r = new B (); // target object
Thread t = new thread (r); // construct a thread object with the target object
Class B implements runable {
Public void run (){
//...
}
}

Thread method

Public static void sleep (long millis) throws interruptedexception
In parentheses, the thread is stopped for a period of time. After the interval expires, the thread may not resume execution immediately.
When Main () is finished, the CPU stops running the time slice even if the time slice is not used up.
.

Try{
Thread.sleep(1000);
}catch ( InterruptedException e){
e.printStackTrace(e);
}


Public final void join () throws interruptedexception
Indicates that other running threads give up the execution right and enter the blocking state until the call thread ends.
In fact, the concurrent thread is converted into a serial operation.


Thread Priority: 1-10. The higher the priority, the higher the priority, the more likely it is to be selected by the OS. (This is not recommended because the priorities of different operating systems are not the same, making the program not cross-platform. This priority is roughly divided).

Public static void field ()
This allows the current thread to immediately hand over the execution right and return to the running state, waiting for the OS to be called again.

Public final Boolean isactive ()
Verify whether the current thread is active, whether or not it is running.

Thread Lifecycle

The following are seven very important states in the thread: (some books only consider the first five states: both the "Lock pool" and "Wait pool" are considered as special circumstances of the "blocking" State: this understanding is also correct, however, separating the "Lock pool" and "Wait pool" separately facilitates the understanding of the program)
1,Initial status, Thread creation, the thread object calls the START () method.
2,Running statusThat is, waiting for CPU resources and running status.
3,Running status, Obtained CPU resources, in the running status.
4,Blocking statusThat is to say, let the CPU resources go into a waiting status, which is not a running status. There are three cases that will go into the blocking status.
1) For example:Waiting for Input(Enter the device for processing, but the CPU does not process), put it into blocking until the input is complete. After blocking ends, it enters the operational status.
2)Thread sleepThe thread object calls the sleep () method. After blocking ends, the thread object enters the running state.
3)Thread object 2 calls the join () method of thread object 1, so thread object 2 enters the blocking state until thread object 1 is aborted..
5,Abort status, That is, the execution is completed.
6,Lock pool status
7,Waiting queue

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.