Process: is a program in progress. Each process executes with an order of execution, which is an execution path, or a control unit
Thread: A separate control unit in a process that controls the execution of a process
Main thread: The JVM starts with a process java.exe. At least one thread in the process is responsible for executing the Java program. And the code that runs the thread exists in the main method, which is called the main thread. Create thread Method 1: define class inheritance thread. The Run method in the replication thread class.
Purpose: Store the custom code in the Run method and let the thread run. The Start method of the calling thread,
Purpose: Start the thread and invoke the Run method.
Class Demo extends thread
{public
void run () //Replication Run method, code
{for
(int x=0; x<60; x + +) to run by the storage thread
System.out.println ("demo Run" +x);
}
Class Test
{public
static void Main (string[] args)
{for
(int x=0; x<4000; x + +)
System.out.println ("Hello world!");
Demo d = new demo (); Create a good thread.
D.start ();//Open the thread and execute the thread's Run method. for
(int x=0; x<60; x + +)
System.out.println ("Hello World!--" +x);
}
Discover that the results of a run are different each time. Because multiple threads are getting the execution power of the CPU. CPU executes to who, who runs.
To be clear, at a certain point, there can only be one program running (except multicore), and the CPU is doing a quick switch to achieve what appears to be a simultaneous operation. We can image the running behavior of multithreading in each other to rob the executive power of the CPU. This is one of the characteristics of multithreading: randomness. Who grabbed who to execute, as to how long the execution, the CPU said calculate. creating a Thread Method 2: defining a class implement the Run method in the Runnable interface of runnable interface overlay
Keep the code that the thread is running in the Run method to establish the thread object through the thread class. Passes the subclass object of the Runnable interface as the actual argument to the constructor of the thread class.
Note: 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 for the specified object, you must be clear about the object to which the Run method belongs. The start method of the thread class is invoked to open the thread and invoke the Run method of the Runnable interface subclass.
Class Demo implements Runnable //Implementation Runnable interface
{
private int x=100;
The public void Run () //Replication Run method, the code
{while (x>0) System.out.println that the storage thread is running (
thread.currentthread () . GetName () + "--run--" +x--);
}
Class Test
{public
static void Main (string[] args)
{
Demo d=new demo ();
Thread t1=new thread (d); Creates two thread objects that pass through thread
t2=new thread (d) of the subclass object of the Runnable interface;
T1.start (); Opens the thread and executes the thread's Run method.
T2.start ();
}
Implementation avoids the limitations of single inheritance, and it is recommended that you use implementation when defining threads.
Two different ways:
Inheritance Thread: Thread code is stored in the thread subclass Run method.
To implement runnable, the thread code has the Run method of the subclass of the interface. Common methods:
Class ThreadTest
{public
static void Main (string[] args)
{
new Thread ()
{public
void run ()
{for
(int x=0; x<100; x + +)
{
System.out.println (Thread.CurrentThread (). GetName () + "..." +x);
}
}
}. Start ();
Runnable r = new Runnable ()
{public
void run ()
{for
(int x=0; x<100; x + +)
{
System.out.println (Thread.CurrentThread (). GetName () + "..." +x);}}
;
New Thread (R). Start ();
}
}
five states of a thread