Multi-thread shared data method record

The use of multi-threading can improve program operation, but if it is not used properly, there may be many bugs in the program.

There are two ways to implement multithreading in java, one is to inherit the Thread class, and the other is to implement the Runnable interface. The second method is generally encouraged.

(Suitable for multiple threads of the same program code to process the same resource; it can avoid the limitation of single inheritance in java; increase the robustness of the program, it can be shared by multiple threads, and the code and data are independent.)

Then call the start method to start the thread. Note: After the start() method is called, the multi-threaded code is not executed immediately, but the thread becomes Runnable, and when to run is determined by the operating system.

In the process of using threads, data synchronization between different threads and preventing deadlocks are usually considered. A deadlock between threads occurs when two or more threads wait for each other to release resources at the same time. In order to prevent deadlocks from occurring, thread safety needs to be achieved through synchronization. The keyword synchronized is used in java.

Many interview questions will ask the difference between threads and processes. Here is a brief summary:

1. A process is a running activity of a program with independent functions on a data set. It is not just the code of the program, but also the current activity, represented by the value of the program counter and the contents of the processing registers.

2. A process is a "program in execution". A program is an inanimate entity, and only when the processor gives the program life can it become an active entity, which we call a process.

3. A thread is an entity of a process, and it is the basic unit of CPU scheduling and dispatch. It is a basic unit smaller than a process that can run independently.

4. Usually a process can contain several threads, which can utilize the resources owned by the process.

5. Processes can share data space exclusively, but threads must share data space.


The difference between synchronous and asynchronous multi-threaded:

Multiple unrelated threads started by a process, the relationship between them is asynchronous. There are four types of asynchronous mechanisms: critical section, mutex, semaphore, and event.

Synchronization: A resource, when thread a is using, thread b cannot access the resource because of the synchronization mechanism.

Asynchronous: A resource that, when thread a is in use, assumes that no synchronization mechanism exists and thread b can access the resource.

Therefore, it can be concluded that synchronization is thread-safe, and asynchronous is thread-unsafe, which is prone to deadlock.


Here are a few test codes for pair threads:



public class Test {


public static void main(String[] args) {
final Data data = new Data();
// new Thread(new add(data)).start();
// new Thread(new dec(data)).start();

new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
data.dec ();
}
}).start();

new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
data.add();
}
}).start();
}


static class Data {
int n = 100;


public synchronized void add() {
n++;
System.out.println(Thread.currentThread().getName() + ":" + n);
}


public synchronized void dec() {
n--;
System.out.println(Thread.currentThread().getName() + ":" + n);
}
}


static class add implements Runnable {


private Data data;


public add(Data data) {
this.data = data;
}


@Override
public void run() {
data.add();
}


}


static class dec implements Runnable {


private Data data;


public dec(Data data) {
this.data = data;
}


@Override
public void run() {
data.dec ();
}


}


}



the second:


public class Test1 {


static int n = 100;


public static void main(String[] args) {
new Thread(new add()).start();
new Thread(new dec()).start();


}


public synchronized static void add() {
n++;
System.out.println(Thread.currentThread().getName() + ":" + n);
}

public synchronized static void dec() {
n--;
System.out.println(Thread.currentThread().getName() + ":" + n);
}

static class add implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
add();
}
}

static class dec implements Runnable{


@Override
public void run() {
// TODO Auto-generated method stub
dec();
}
}
 
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326317242&siteId=291194637