201711671224 "Java Programming" chapter 12 learning summary

JAVA thread

A thread is smaller than the process execution unit, a process in its execution process, you can generate multiple threads, forming a plurality of execution clues, each clue that each thread has its own production, existence and demise of the process.

Multithreading refers to an application executable at the same time there are several, according to the situation a few different execution clues to work together, which allows programmers to easily develop multithreaded issue function, handling multiple tasks simultaneously powerful s application.

Although execution threads a feel for several events occur simultaneously, but the computer at any time can only execute more than one thread, just to control the JVM can quickly switch from one thread to another.

Each JAVA application has one main thread (main thread), when the main thread running if no other thread is created, the end of the thread, if created, then wait until all the threads run ends before the end of the program.

 

Processes and threads

The fundamental difference: the process is the basic unit of the operating system resource allocation, and the thread is the basic unit of task scheduling and execution of

In terms of overhead: each process has its own code and data space (application context), switching between programs have larger overhead; thread can be seen as lightweight processes, threads share the same type of code and data space each thread has its own independent runtime stack and program counter (PC), small switches between threads overhead.

The environment: run multiple processes (programs) in the operating system at the same time; and there are multiple threads in the same process (program) simultaneously perform (by CPU scheduling, the film has only one execution thread at each time)

Memory allocation: When the system is running a different memory space will be allocated for each process; and thread, in addition to the CPU, the system will not (from that resource belongs to the process of resource thread used) to allocate memory for the thread You can share resources, thread group.

Containment relationship: no thread can be seen as a single-threaded process, if there are multiple threads within a process, perform the procedure is not a line, but a plurality of lines (threads) together to complete; the thread is part of the process, so the thread is also known as light weight process or LWP.

 

The relationship between the target object and thread

1) the target object and thread completely decoupled

- the target object is not the target object is often a combination of thread object need to obtain the name of the thread (because they can not get the thread object reference) in order to determine which thread is CPU resources, ie JVM thread being executed.

2) a combination of thread Audience (weak coupling)

- The target objects can be combined when the target object class combination thread thread object, the target object can be obtained by reference to the thread object.

 

Create a Thread class and thread

In the Java language, create a thread object with the Thread class or subclass. When writing a subclass of the Thread class, need to override the parent class run () method, which aims to specific operational requirements threads, or threads do nothing, because the parent class run () method does not have any operating statement . Another way to create a thread is to create a thread object directly with the Thread class. Use Thread to create threads commonly used constructor is: Thread (Runnable target).

The constructor parameter is a Runnable interface type.

Must be transmitted when creating a thread object to the constructor argument an instance of a class that implements Runnable interface, called an object instance of the object is created by the target thread after thread calls start () method, once it is its turn to access to CPU resources, audience will run () method (callback Interface) interface is automatically called.

 

Common method thread

1. start (): thread calls the method will start the thread, so that the new state into the ready queue line up, turn it once to enjoy CPU resources, you can create it from the thread to start their own independent life cycle of.

2. run (): the same function and role of the Thread class run () run Runnable interface method () method, threads are used to define the operation object is performed after the scheduling, the system is automatically calls the user program can not be referenced Methods.

3. sleep (int millsecond): high priority thread can call sleep method in its run () method to make himself give up CPU resources, sleep for some time.  

4. isAlive (): thread in "new" condition, thread calls isAlive () method returns false. Prior to run until the end of the thread method that did not enter the state of death (), thread calls isAlive () method returns true.

5. currentThread (): This method is a class method of the Thread class, you can use the class name called, which returns the thread is currently using the CPU resources.  

6. interrupt (): a thread occupy CPU resources can make sleeping thread calls interrupt () method "awakened" themselves, that lead to the occurrence of dormant threads InterruptedException anomaly, ending dormancy, re-queue waiting for CPU resources.     

 

Thread Synchronization

In dealing with multi-threading issues, we must pay attention to this question: When two or more threads simultaneously access the same variable, and a thread needs to change this variable. We deal with such problems to deal with.

In dealing with thread synchronization, first thing to do is to take the data modification method with the keyword synchronized to modify.

The so-called thread synchronization is the number of threads need to use a synchronized modification method.

 

Coordinating thread synchronization

wait () method can interrupt method of execution, so that the thread waits temporarily give up the right to use the CPU, and allow other threads to use this synchronization method.

notifyAll () method notifies all of the synchronization due to the use of this method in the waiting thread waiting for the end. He was interrupted thread will continue to execute the synchronization method from just left off, and follow the "first-break to continue" principle.

notify () method only notice in the waiting thread one end of the wait.

 

Joint thread

A thread during a possession CPU resources, allowing other thread calls join () and this thread joint, such as: B.join ();

A joint operation, said during a B. If the thread occupy CPU resources during A joint once the thread B, then A thread will immediately interrupt the execution, wait until it is finished joint thread B, A thread re-queued for CPU resources, in order to resume execution. If A and B jointly prepared thread has ended, B.join () will not have any effect.

 

 GUI thread

When the Java program contains a graphical user interface (GUI), Java virtual machine will start more threads automatically when you run the application, which has two important threads: AWT-EventQuecue and AWT-Windows. AWT-EventQuecue thread is responsible for handling GUI events, AWT-Windows thread is responsible for drawing form or component to the desktop. JVM opportunity to ensure that each thread has its CPU resources, for example, when the GUI event in the program, JVM will switch the CPU resources to the AWT-EventQuecue thread, the thread will AWT-EventQuecue to handle this event, for example, you click on the button in the program, triggering ActionEvent event, AWT-EventQuecue thread immediately queued for execution of the code to handle events   

 

The timer thread

A timer every millisecond "ringing" a, b is the parameter monitor timer. Ringing timer events occur is ActinEvent type of event. When the ringing event occurs, the event monitor will be monitored, to monitor callback ActionListener interface actionPerformed (ActionEvent e) method. Use start Timer class () starts a timer that starts a thread. Use the Timer class method stop () stops the timer that suspends the thread, using the restart () to restart the timer, the recovery thread.

Daemon thread

A thread calls the void setDaemon (boolean on) method can set itself as a guardian (Daemon) threads, such as: thread.setDaemon (true);

When all the user-threaded programs have been the end of the run, even if the statement of the run method daemon thread there need to be performed, but also an immediate end to run a daemon thread

Guess you like

Origin blog.csdn.net/nemeziz/article/details/84522459