"Process"
The <1> process is a static concept (code that is loaded into the memory code area, which represents a process).
<2> threads are different execution paths in a process, and threads are running on the machine. <3> for a single CPU, at the same point in time, the CPU only supports one thread to run. True multithreading is dual CPU or dual core. "Processes and Threads" <1> Each process has its own code and data space (process context), and switching between processes can have a significant overhead. <2> threads can be thought of as lightweight processes, sharing code and data spaces with a class of threads, each with a separate run stack and program counter (PC), thread switchingis less expensive. <3> Multi-process can run multiple tasks (programs) concurrently in the operating system. <4> Multithreading executes simultaneously in multiple sequential streams in the same application. "description" <1>java threads are implemented through the Java.lang.Thread class. <2>VM starts with a thread defined by the main method (public static void Main () {}). <3> can create new threads by creating new thread instances (under the premise of implementing the corresponding interfaces). <4> Each thread completes its operation by means of the method run () that corresponds to a particular thread object, and the method run () is called the thread body. <5> starts a thread by invoking the Start method of the thread class. "Thread creation and startup" note:<1> runs the thread (both alternately or side-by-side) in the case where the main method continues execution. <2> These two methods recommend using the interface implementation method, because the use of inheritance can no longer implement other classes (Java does not support multiple inheritance), and implement an interfaceIt is also possible to inherit classes or implement other interfaces so that they are easy to use in the real world (as long as the interfaces do not use class inheritance). "Thread state Transitions" Note:<1> calls the start () method does not necessarily execute the thread, but is in a ready state. <2> is in the running state after it has been transferred to the CPU. "Thread Common methods" Note: The <1>new is not start, it is "dead", the ready state, the running state, the blocking state are all "live". "Sleep/join/yield Method" ----Sleep Method<1> can invoke the static method of thread:Public static void sleep (Long Mills) throws Interruptedexception//causes the current thread to hibernate (temporarily stop mills milliseconds)//must throw an exception (it is an exception when the sleep state is awakened)"program"Public static void Main (string[] args) {MyThread thread = new MyThread ();Thread.Start ();try {thread.sleep (+);}catch (Interruptedexception e) {}Thread.Interrupt ();//Shut down thread (this is not recommended, and it is not recommended to use the stop,windows process to kill the method) } class MyThread extends thread{Public void Run () {While (true) {System.out.println (New Date ());try{sleep (+);}catch (interruptedexception e) {return; } } }Note: It is not possible to throw an exception on the Run method, because run is an overridden method, and the overridden method cannot throw an exception that is not the same as the overridden method, which can only beuse the Try-catch statement. <2> because it is a static method, sleep can be called directly by the class name:Thread.Sleep (); ----Join Methodmerging multiple threads (equivalent to a method call, no longer running synchronously in the original multi-thread) ----Yield Methodgive the CPU a chance to execute on other threads. "Thread Priority" <1>java provides a thread scheduler to monitor all threads in the program that have entered a ready state after being started. the thread scheduler determines which thread should be dispatched to execute according to the priority of the thread. the priority of the <2> thread is represented by a number, ranging from 1 to 10, and the default priority for a thread is 5.thread.min_priority = 1;thread.max_priority = ten;thread.norm_priority = 5;<3> Use the following method to get or set the priority of the thread object:int getpriority ();void setpriority (int newpriority); "Thread Synchronization"
java--Threads