1, the basic concept of threading
A simplified view of the computer is that it has a processor that performs the calculation, a process that contains the processor
Rom (read-only memory), RAM (read-only memory) that contains the data to be manipulated by the program. Thread, is considered to be with its own path
And the package of the pseudo-processor for the sequence code and data. Three parts of a thread processor, code, data.
The code can or may not be shared by multiple threads, which is independent of the data. Two threads if you perform the same class
Instance code, they can share the same code.
Similarly, the data can or may not be shared by multiple threads, which is independent of the code. Two threads if a shared pair
Access to a common object, they can share the same data.
Threading is a tool that allows multiple activities to coexist in a process. Threads are also known as lightweight processes. As with processes, threads are independent, concurrent execution paths in the program, each with its own stack, its own program counter, and its own local variables. Multiple threads in a process share the same memory address space, which means they can access the same variables and objects, and they allocate objects from the same heap.
Each Java program has at least one thread-the main path. When a Java program starts, the JVM creates the main thread and invokes the program's main () method in that thread. The JVM also creates other threads that you typically do not see-for example, threads related to garbage collection, object termination, and other JVM housekeeping tasks.
2, some reasons for threading
make the UI more responsive and take advantage of multiprocessor systems.
The differences between threads and processes are:
Each process has its own code and data space (process context), and the process switching overhead is high.
Threads as lightweight processes, the same class of threads can share code and data space, but each thread has a single
Running stacks and program counters, so the overhead of thread switching is small.
Multi-process-the ability to run multiple tasks (programs) and multiprocessing tasks in the operating system.
Multithreading-Multiple sequential streams are executed simultaneously in the same application.
3. Threads in Java programming
A Thread class constructor takes a parameter, which is an instance of Runnable. A Runnable is generated by a class that implements the Runnable interface (that is, provides a public void run () method).
A multithreaded programming environment allows you to create multiple threads that are based on the same Runnable instance. This can be done through the following
method to do this:
Thread t1=new thread (r);
Thread t2=new thread (r);
At this point, the two threads share data and code.
3.1 Start thread
A newly created thread does not automatically start to run. You must call its start () method. Calling the start () method causes the virtual processor that the thread represents to be in a running state, which means it can be dispatched and executed by the JVM. This does not mean that the thread will run immediately.
3.2 Thread Status
(1) New status-ready state:
When the programmer explicitly invokes the thread's start () method, the thread enters the ready (Runnable) state, which is also known as the operational state.
(2) Ready state-operating status:
The thread scheduler provided by the Java runtime system is scheduled according to certain rules, but once a thread obtains an execution opportunity, it immediately enters the running (Running) state and begins executing the thread body code.
(3) Operating state-blocking state:
When a running state thread is blocked, the scheduler immediately dispatches another thread in the ready queue to start running.
(4) Blocking state-ready state:
When a blocked thread waits for a condition that is already available, such as when a user input operation has completed, the thread will unblock and go into a ready state. Note that instead of resuming execution, go back to the ready queue to line up.
(5) Operating status-termination status
When the thread's run () method finishes executing, its operation ends naturally, and the thread enters the terminating (Dead) state. You can also terminate the execution of a thread, such as calling its stop () method, during a run. A thread that is in the terminating state cannot be rerun, so it is not allowed to call the start () method two times on a thread object.
Java Multithreaded Learning Note 1