When we learn the operating system, we learn about processes and threads, so what are the 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.
thread is the smallest unit in which the operating system is able to perform operational scheduling.
It is included in the process and is the actual operating unit of the process.
A thread refers to a single sequential control flow in a process in which multiple threads can be concurrent and each thread performs different tasks in parallel.
How Java programs work
The Java command launches the Java Virtual machine, starting the JVM, which is equivalent to launching an application, that is, initiating a process. The process automatically starts a "main thread" and then the main thread calls the main method of a class.
So the main method runs in the main thread. All programs prior to this are single-threaded.
Is the startup of the JVM multithreaded?
1, Java command to start JVM,JVM will start a process that will start a main thread.
2, the JVM startup is multithreaded, because it has a minimum of two threads started, the main thread and the garbage collection thread.
Multi-threaded Implementation scheme
1. Inherit the thread class
/*** This class is going to rewrite the run () method, why? * not all code in the class needs to be executed by the thread. * And at this point, in order to distinguish which code can be executed by the thread, Java provides a run () in the thread class to contain code that is executed by the thread. * * @authorSun **/ Public classMyThreadextendsThread {Private inti; PublicMyThread (inti) {Super(); This. i =i; } @Override Public voidrun () {System.out.println ("I=" +i); }}
/** * Multithreaded Test * * @author Sun * */public class ThreadTest {public static void main (string[] args) {MyThread myThread1 = New MyThread (1); MyThread myThread2 = new MyThread (2); MyThread myThread3 = new MyThread (3); MyThread myThread4 = new MyThread (4); MyThread myThread5 = new MyThread (5); Mythread1.start (); Mythread2.start (); Mythread3.start (); Mythread4.start (); Mythread5.start ();//Because the run () method directly calls is actually equivalent to the normal method call, so you see the single-threaded effect Mythread1.run ();}}
By the code, we can also see that the code in the thread can also be called with the Run method.
So where is the difference between run () and start ()?
Run (): Just encapsulate the code executed by the thread, the direct call is the normal method
Start (): Starts the thread first, and then the JVM calls the thread's run () method.
2. Implement Runnable interface
public class Myrunnable implements Runnable {@Overridepublic void run () {SYSTEM.OUT.PRINTLN ("Running!");}}
/** * * @author Sun * */public class Run {public static void main (string[] args) {Runnable Runnable = new Myrunnable () ; Thread thread = new Thread (runnable); Thread.Start ();}}
Benefits of implementing an interface runnable approach
You can avoid the limitations of Java single inheritance.
Suitable for multiple same program of code to deal with the same resource situation, the thread with the program code, data effectively separated, better embodies the object-oriented design ideas.
There are two scheduling models for threads:
The time-sharing scheduling model uses CPU's use of the thread in turn, evenly allocating the CPU-
Preemptive scheduling models prioritize high-priority threads to use the CPU, and if the threads have the same priority, they randomly select a higher-priority thread to get more CPU time slices.
Java uses a preemptive scheduling model.
So we already know the scheduling of threads, then we can use the following methods to control the object thread
Thread hibernation public static void sleep (Long Millis): Allows the currently executing thread to hibernate within the specified number of milliseconds (pausing execution
Thread joins public final void join (): Waits for the thread to terminate.
Thread comity public static void Yield (): Pauses the currently executing thread object and executes other threads.
Middle thread public final void Stop (): obsolete. This method has inherent insecurity.
public void Interrupt (): Middle break thread.
/** * Thread Hibernation * * @author Sun * */public class Threadsleepdemo {public static void main (string[] args) {Threadsleep ts1 = New Threadsleep (); Threadsleep ts2 = new Threadsleep (); Threadsleep Ts3 = new Threadsleep () Ts1.setname ("Sun"); Ts2.setname ("tin"); Ts3.setname ("ting"); Ts1.start (); Ts2.start (); Ts3.start ();}} Class Threadsleep extends Thread {@Overridepublic void Run () {for (int x = 0; x < n + +) {System.out.println (getName () + ":" + x + ", Date:" + new Date ());//sleep, set 1 seconds try {thread.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();}}}}
public class Threadjoindemo {public static void main (string[] args) {Threadjoin tj1 = new Threadjoin (); Threadjoin tj2 = new Threadjoin (); Threadjoin tj3 = new Threadjoin () tj1.setname ("1"); Tj2.setname ("2"); Tj3.setname ("3"); Tj1.start (); try {tj1.join ();} catch (Interruptedexception e) {e.printstacktrace ();} Tj2.start (); Tj3.start ();}} Class Threadjoin extends Thread {@Overridepublic void Run () {for (int x = 0; x < n + +) {System.out.println (GetName ( ) + ":" + x);}}}
/*** Pauses the currently executing thread object and executes other threads. * @authorSun **/ Public classThreadyielddemo { Public Static voidMain (string[] args) {Threadyield ty1=NewThreadyield (); Threadyield Ty2=NewThreadyield (); Ty1.setname ("Brigitte"); Ty2.setname ("Elina"); Ty1.start (); Ty2.start (); }}classThreadyieldextendsThread {@Override Public voidrun () { for(intx = 0; x < 100; X + +) {System.out.println (GetName ()+ ":" +x); Thread.yield (); } }}
/* Public final void Stop (): Let the thread stop and compare the violence. It is not recommended. * public void Interrupt (): Middle Thread. Terminates the state of the thread and throws a interruptedexception. */public class Threadstopdemo {public static void main (string[] args) {threadstop ts = new Threadstop (); Ts.start (); try {Th Read.sleep (+);//Ts.stop (); Ts.interrupt ();} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Threadstop extends Thread {@Overridepublic void run () {try {thread.sleep (10000);} catch (Interruptedexception e) {}} }
Let's talk about it.
Java Foundation 07 Multi-Threading