Tag: Bre IMA top 51cto implements system run div www.
Process: Each process has its own code and data space (process context), and there is a significant overhead of switching between processes, and a process contains 1--n threads.
Threads: The same class of threads share code and data space, each thread has a separate run stack and program counter (PC), and thread switching overhead is small.
Threads and processes are divided into five phases: Create, ready, run, block, terminate.
Multi-process means that the operating system can run multiple tasks (programs) at the same time.
Multithreading refers to having multiple sequential streams in the same program executing
In Java to implement multi-threading, there are two ways, one is to continue the thread class, and the other is to implement the Runable interface.
The thread class is defined in the Java.lang package. A class that inherits the thread class to overwrite the run () method in this class can implement multithreaded operations, but only one parent class can inherit from a class, which is the limitation of this method. One, the multi-threaded establishment way thread&runnable respectively sells the train ticket 1, the thread way
classMyThreadextendsthread{Private intticketscont=5;//There are 5 train tickets altogether . PrivateString name;//window, which is also the name of the thread PublicMyThread (String name) { This. name=name; } @Override Public voidrun () { while(ticketscont>0) {Ticketscont--;//If there's a ticket, sell a ticket.System.out.println (name+ "sold 1 tickets, the remaining votes:" +Ticketscont); } }} Public classticketsthread{ Public Static voidMain (String args[]) {//create three threads, simulate three windows to sell a ticketMyThread mt1=NewMyThread ("Windows 1"); MyThread mt2=NewMyThread ("Windows 2"); MyThread Mt3=NewMyThread ("Windows 3"); //start three threads, that is, windows, start selling ticketsMt1.start (); Mt2.start (); Mt3.start (); }}
This allows the program to complete the interactive operation normally. So why not use Start ();
Under the JDK installation path, Src.zip is the entire Java source program, and this code finds the definition of the start () method in thread, and you can see that the private native void Start0 () is used in this method; Where the Native keyword means that the underlying function of the operating system can be called, then such a technique becomes a JNI technology (Java Native Interface). 2, Runnable Way
Steps:
1, define class implementation runnable interface
2, covering the Run method in the Runnable interface
Store the code that the thread will run in the Run method
3, thread objects are established through the thread class
4, pass the subclass object of the Runnable interface as the actual parameter to the constructor of the thread class
Why pass a subclass object in the Runnable interface to the thread's constructor
Because the object that the custom run method belongs to is the subclass object of the Runnable interface
So to have the thread specify the run method of the specified object, you must explicitly define the object to which the Run method belongs.
5, call the Start method of the thread class to open the thread and invoke the Run method of the Runnable interface subclass
classMyThread2Implementsrunnable{Private intticketscont=5;//There are 5 train tickets altogether .@Override Public voidrun () { while(true){ synchronized( This){ if(ticketscont<=0){ Break; } Ticketscont--;//If there's a ticket, sell a ticket.System.out.println (Thread.CurrentThread (). GetName ()+ "Sold 1 tickets, remaining votes:" +Ticketscont); /*try{Thread.Sleep (50); To view the effect by blocking the program} catch (Exception e) {System.out.println (e); }*/ } } }} Public classticketsrunnable{ Public Static voidMain (String args[]) {MyThread2 MT=NewMyThread2 (); //create three threads to simulate three ticketing windowsThread th1=NewThread (MT, "Window 1"); Thread Th2=NewThread (MT, "Window 2"); Thread Th3=NewThread (MT, "Window 3"); //start three threads, that is, three windows, start selling ticketsTh1.start (); Th2.start (); Th3.start (); }}
However, there is no start () method in a subclass defined with runnable, only in the thread class. At this point the thread class is observed, and there is a constructor method: Public Thread (Runnable Targer) This construction method accepts the subclass instance of Runnable, which means that the thread class can be used to start the multithreading of the Runnable implementation. (Start () can coordinate the resources of the System): Ii. Summary
- Create: Creates a new thread object, such as thread thd=new thread ()
- Ready: After the thread object is created, the thread's start () method is called (at which point the thread knowledge enters the thread queue, waits for the CPU service, has the running condition, but does not necessarily start running)
- Run: A thread in the ready state, once the CPU resource is acquired, it goes into the running state and starts executing the logic inside the Run () method
- Terminate: The thread's Run () method finishes executing, or the thread calls the Stop () method, and the thread enters the terminating state
- Blocking: An executing thread, in some cases, temporarily yielding CPU resources for some reason, pausing its own execution and entering a blocking state, such as calling the Sleep () method
On thread-based multithreading (example 1), a total of three windows, sold 5 tickets in each window, sold a total of 15 tickets, unable to achieve resource sharing, resulting in a program error.
On the basis of runnable is, using synchronous sharing, to achieve resource sharing, the three windows sold a total of 5 tickets.
We recommend that you create threads this way using runnable. The same resource in the program refers to the same Runnable object. It is necessary to join the synchronous synchronized in the secure ticket selling procedure.
Reference documents
1, Java Multithreading thread VS runnable detailed
2. The difference between runnable and thread in Java
The differences between Java threading-thread and runnable