==================== Concepts of programs, processes, and Threads ==================
1. What are the differences between programs, processes, and threads?
A process is a program with a certain independent function about a single run activity on a data set, a process that is an independent unit of the system's resource allocation and scheduling.
A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process. The thread itself basically does not own the system resources, only has a point in the operation of the necessary resources (such as program counters, a set of registers and stacks), However, it can share all of the resources owned by the process with other threads that belong to one process.
Relative to a process, a thread is a concept that is closer to the execution body, it can share data with other threads in the process, but has its own stack space and has a separate execution sequence.
The main difference between processes and threads is that they are different ways to manage operating system resources. The process has a separate address space, and after a process crashes, it does not affect other processes in protected mode, and the thread is just a different execution path in a process. Threads have their own stacks and local variables, but there is no separate address space between the threads
In short, a program has at least one process, and a process has at least one thread.
Program Preheating:
First of all to understand, such as the next program is not multithreaded?
public class Multithread {
public static void Main (string[] args) {
Method2 ();
}
public static void Method1 () {
System.out.println ("Method1 carried out! ");
}
public static void Method2 () {
System.out.println ("METHOD2 carried out! ");
Method1 ();
}
}
Creation and use of multi-threading in Java
Inheriting the thread class and implementing the Runnable interface
1. Inheriting the thread class
1 The first way to implement multithreading, inheriting the thread class
Class Subthread extends thread{
Need to rewrite the Run method
public void Run () {
for (int i = 0; I <100;i++) {
System.out.println (Thread.CurrentThread (). GetName () + ":" +i);
}
}
}
public class Multithread {
public static void Main (string[] args) {
Subthread subthread = new Subthread ();
Open Thread
The same child thread can only be called once!
Subthread.start ();
for (int i = 0; I <100;i++) {
System.out.println (Thread.CurrentThread (). GetName () + ":" +i);
}
}
}
Realizing the function of multi-window ticketing by inheriting the thread class
1 Mock train station ticketing window, open three-window ticket, total number of tickets is 100
Class Window extends thread{
private static int ticket = 100;
"Non-static properties are not shared by multiple threads!" 】
public void Run () {
while (Ticket > 0) {
System.out.println (Thread.CurrentThread (). GetName () + "sold Ticket:" + (ticket--));
}
}
}
public class Multithread {
public static void Main (string[] args) {
Window w1 = new Window ();
Window W2 = new Window ();
Window W3 = new window ();
W1.start ();
W2.start ();
W3.start ();
}
}
The second way to implement multithreading: how to Implement Interfaces
2. The second way to implement the thread, the way to implement the interface
Class Subthread implements runnable{
@Override public void Run () {
for (int i=0; i<100;i++) {
System.out.println (Thread.CurrentThread (). GetName () + ":" +i);
}
}
}
public class Multithread {
public static void Main (string[] args) {
Subthread S1 = new Subthread ();
thread T1 = new Thread (S1);
Thread t2 = new Thread (S1);
T1.start ();
T2.start ();
}
}
Simulation window selling tickets. The second way to implement the thread, the way to implement the interface
Class Subthread implements runnable{
private int ticket = 100;
@Override
public void Run () {
while (ticket >0) {
System.out.println (Thread.CurrentThread (). GetName () + "The tickets sold are:" + (ticket--));
}
}
}
public class Multithread {
public static void Main (string[] args) {
Subthread S1 = new Subthread ();
thread T1 = new Thread (S1);
Thread t2 = new Thread (S1);
T1.start ();
T2.start ();
}
}
Contrast: What is the better way to inherit the thread class and implement the Runnable interface? The way to achieve more elegant:
①: Avoids the limitations of Java single inheritance
②: If multiple threads are working on the same resource (or data). It is more appropriate to use the implementation method.
==================== Multithreading Security Issue "Demo" ========================
This program has a thread safety problem, there is a problem of re-ticket or wrong ticket!
Class Subthread implements runnable{
private int ticket = 100;
@Override public void Run () {
while (ticket >0) {
try {
Thread.Sleep (10);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println (Thread.CurrentThread (). GetName () + "The tickets sold are:" + (ticket--));
}
}
}
public class Multithread {
public static void Main (string[] args) {
Subthread S1 = new Subthread ();
thread T1 = new Thread (S1);
Thread t2 = new Thread (S1);
T1.start ();
T2.start ();
}
}
So how do you deal with thread-safety issues?
1. First, what is the cause of the thread safety problem?
Because of the fact that one thread is working on the shared data and is not done, another thread is involved, resulting in a security problem with the shared data.
2. How do I troubleshoot thread security issues?
Once you have a thread operation to share the data, other threads have the opportunity to participate in the shared data operation!
3.Java How to implement thread security: thread synchronization mechanism
Mode one: Synchronizing code blocks [Reference DOC documents]
Mode two: Synchronization method [The default lock object is the This keyword
]
20171016-Process, threading issues