What Is a Thread Machine?
A thread is the smallest unit that the operating system can use to schedule operations. It is included in the process and is the actual operating unit in the process. A thread refers to a single-order control flow in a process. A process can have multiple threads concurrently, and each thread executes different tasks in parallel. It is also called lightweight processes in Unix System V and SunOS, but lightweight processes refer more to kernel threads and user threads to threads.
Thread
- Thread is
- Introduction of threads:
- In the 1960s, the basic unit capable of owning resources and operating independently in the OS was a process. However, with the development of computer technology, many shortcomings have occurred in the process. First, because the process is the resource owner, there is a large amount of creation, cancellation, and switching. Space-time overhead, so light processes need to be introduced. Second, due to the emergence of symmetric multiprocessors (SMPs), multiple running units can be satisfied, and the parallel overhead of multiple processes is too large.
- Therefore, in the 1980s, the basic unit that could run independently-Threads appeared.
- In the server
- Daemon threads are special threads that are generally used to serve other threads in the background.
- In Java, isDaemon (): determines whether a thread is a daemon thread.
- In Java, setDaemon (): Set a thread as a daemon thread.
- C # daemon thread
- Class 1: daemon thread class
/ ** * This thread sets a timeout * After the thread starts running, after the specified timeout period, * the thread will throw an unchecked exception to notify the calling program of the thread to timeout * You can call this class before the timeout cancel method cancels the timer * @author solonote * / public class TimeoutThread extends Thread {/ ** * Timer timeout time * / private long timeout; / ** * Whether the timer is cancelled * / private boolean isCanceled = false; / ** * Exception thrown when the timer expires * / private TimeoutException timeoutException; / ** * Constructor * @param timeout specifies the timeout time * / public TimeoutThread (long timeout, TimeoutException timeoutErr) {super (); this.timeout = timeout ; this.timeoutException = timeoutErr; // Set this thread as a daemon thread this.setDaemon (true);} / ** * Cancel timing * / public synchronized void cancel () {isCanceled = true;} / ** * Start timeout timer * / Public void run () {try {Thread.sleep (timeout); if (! IsCanceled) throw timeoutException;} catch (InterruptedException e) {e.printSta ckTrace ();}}}
- Thread synchronization is Java
- (1) Create a thread [6]
- When a new process is created, a new thread is also created, and threads in the process can create new threads in the same process.
- Create a thread
- (2) Terminate the thread
- You can terminate yourself normally, or you may be forced to terminate by another thread due to a thread execution error. Terminating the thread operation is mainly responsible for releasing the registers and stack occupied by the thread
- (3) blocking threads
- When the thread waits for each event to fail, it stops running.
- Blocking thread
- (4) Wake up the thread
- When an event that blocks a thread occurs, the blocked thread state is set to the ready state, and it is hung on the ready queue. Processes still have execution-related states. For example, the so-called process is in the "executing" state, which actually means that a thread in the process is executing. Operations related to the state of a process are also applied to its threads. For example, when a process is suspended, all threads in the process are also suspended, as is activation.