java study notes (Basics) - Thread

First, the threads and processes

A thread is the smallest unit of program execution, it is dynamic.
The process is the smallest unit of resource allocation system processes and system scheduling is dynamic.
Of threads and processes: Multitasking ---> High efficiency

Simultaneity

At the same point in time you can only execute a process or thread
in the same period of time can execute multiple processes or threads

Data is not shared multi-process, each process has its own separate memory space. Data multithreaded process data is shared, each thread has its own independent stack space area. Is an independent between threads and threads are not affected each other.

A program can start multiple processes
A process can start multiple threads
threads must start the process, can not exist independently.

.class runs on the JVM, .class is a one thread. JVM is a process.

Three elements thread: Two

CPU:

Running threads, all threads share the CPU. Only seize thread CPU will execute successfully.

Code:

Code executed during the running thread blocks. The method will be executed after the thread starts public void run () is.

data:

1) local variable is not shared multithreaded
2) extends Thread multithreaded manner, the static variable is shared, instance variables do not share
3) implements Runnable multithreaded manner, the static variable is shared, sharing instance variables

3: Use thread

The first way:
class MyThread extends Thread{
    public void run(){
    }
}

建线程:Thread t1=new MyThread();
//不要t1.run()来企图启动线程,那是单纯的调用该方法,并没有启动一个线程
启动线程: t1.start();
The second way:
class MyThread implements Runnable{
    public void run(){
    }
}
创建线程:MyThread m=new MyThread();
    Thread t1 = new Thread(m);
启动线程: t1.start();
Thread class Method:

getName (): Get the current thread's name
static sleep (long): Forced currently executing thread to sleep (suspended), just let the CPU, but will not release the lock.
static currentThread (): Gets the currently executing thread.

The method of Thread class:
getName (): Get the current thread's name
static sleep (long): Forced currently executing thread to sleep (suspended), to "slow down the thread."
static currentThread (): Gets the currently executing thread.

note:

Preemptive running threads, the thread so the results are unpredictable.

Four: Why have two ways

java single inheritance

class Test extends Student implements Runnable{
} 

Five: thread state

a) new state (new): thread object has been created, there is no call to start () method on it.

b) the ready state (Runnable): thread is ready, waiting for the CPU scheduling, when the thread qualify row, but the scheduler has not selected it in when the thread is a thread running state. When the start () method is called, the thread first enters the ready state. After the thread from blocking or running, or wait back to sleep, also returns to the Ready state. Ready state maintains a thread pool. All threads will enter the ready state to the thread pool.

c) operating state (Running): thread scheduler selecting a thread in a time as the current state of the thread pool from a running. This is also the only way to thread into the running state. CPU executing thread.

d) waiting / blocking / sleep: This is the state it was in when threads are eligible to run. In fact, this combination as a three-state, whose common denominator is: the thread is still alive, but is not currently running condition. In other words, it is run, but if something occurs pieces, he may return to the ready state.

e) the state of death: When the thread run () method completes mean that it is dead. The thread object might be alive, but it has not a single thread of execution. Upon the death of threads, it is final. If you call start on a dead thread () method throws an exception java.lang.IllegalThreadStateException.

Six: interrupt thread: thread to normal end to the state of death

1) using a flag variable interrupt thread. Just like driving a car.
2) using the stop () is obsolete, although it did stop a running thread, however, this method is unsafe and was advocated.

Thread class Method:

join (): execution of waiting threads end. Who would call waiting in which thread. Who calls the method on who wait.
interrupt (): interrupt blocking the thread. Executing thread can not be interrupted.
isInterrupted (): determine whether the interrupted
static interrupted (): Empty interrupt information

Seven: concurrent access threads: multiple threads simultaneously operate the same object.

synchronized (shared area)} {critical region
shares: simultaneous multi-threading operating the same object
critical area: multithreaded code region shared operating region
will affect all the methods are defined to be thread-safe security thread becomes the type. can be synchronized on the way, it is a synchronization method.

Genlock there are many, here it is a simple description of what the synchronized keyword, and details about the specific use, follow-up will launch a special article concerned.

Welcome to my community Tencent cloud +: Portal

Guess you like

Origin www.cnblogs.com/chlinlearn/p/11246290.html