Detailed explanation of java threads and the difference between threads and processes
1. Processes and threads
Each process has a unique memory space, and an application can start multiple processes at the same time. For example, in IE browser, opening an Ie browser is equivalent to starting a process.
A thread refers to an execution process in a process, and a process can contain multiple threads.
Each process needs the operating system to allocate independent memory space to it, and multiple threads in the same process share this space, that is, shared memory and other resources.
Every time java.exe is called, the operating system will start a Java virtual machine process. When the Java virtual machine process is started, the Java virtual machine will create a main thread, which will start executing from the program entrance main method.
Every time a Java virtual machine starts a thread, it will assign a thread method stack to the thread to store relevant information (such as local variables, etc.), and the thread runs on this stack. Therefore, local variables in Java objects are thread-safe, but instance variables and class variables are not stored in the stack, so they are not thread-safe.
There are three states of the process: ready, execution, and blocking.
2. Thread creation method
Runnable method: (This method is flexible, recommended)
public class Thread02 implements Runnable { public static void main(String[] args) { Runnable r = new <strong>Thread02</strong>(); Thread t1 = new Thread(<strong>r</strong>, "t1"); /** * Thread source code* public Thread(Runnable target, String name) { init(null, target, name, 0); } */ Thread t2 = new Thread(r, "t2"); t1.start(); // Start thread t1, in ready state, waiting for cpu t2.start(); // Start thread t2, in ready state, waiting for cpu t1.run(); // The main thread main calls the run method of object t1} public void run() { System.out.println("thread's name is " + Thread.currentThread().getName()); } }The result of the operation may be:
thread's name is t1thread's name is mainthread's name is t2
The head way
public class Thread03 extends Thread { public static void main(String[] args) { Thread03 t1 = new <strong>Thread03</strong>(); //Write it as Thread t1=new Thread() Note: Thread03 is a thread at this time t1.start(); } public void run() { System.out.println("thread's name is " + Thread.currentThread().getName()); }}Running result: thread's name is Thread-0
Note: In addition to the customized threads, there is also a main thread every time the program runs.
comprehensive:
public class Thread01 { public static void main(String[] args) { Thread thread=new Thread(); thread.start();//What really works is run() /** and run in Thread * public void run() { if (target != null) { target.run(); } } So the thread you created yourself needs to rewrite the run method and put the content to be executed into run(), so you need to implement the interface or inheritance and then generate subclasses*//How to create thread 1 thread subclass method (inheritance) Thread thread1=new Thread(){ public void run() { while(true){ try { Thread.sleep(500);//Rest for 500ms} catch (InterruptedException e) { e.printStackTrace(); } //Thread.currentThread() gets the current thread System.out.println("Thread 1's name is "+Thread.currentThread().getName()); } } } };// thread1.start(); //Not able to start without writing the thread//How to create thread 2 runnable method (implementation) It is recommended to use Thread thread2=new Thread(new Runnable(){ public void run(){ while(true){ try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("thread2'name is "+Thread.currentThread().getName()); } }});// thread2.start(); //execute thread new Thread(new Runnable(){ public void run() { System.out.println("runnable "+Thread.currentThread().getName()); }}){ public void run() { //The run method in the subclass overrides the run method in the parent class, so that the runnable will not be executed System.out.println("thread "+Thread.currentThread().getName()); } }.start(); } /*** * Execution of multithreading in a single CPU is very likely to reduce execution efficiency rather than improve one person doing the same thing in different places*/}Thank you for reading, I hope it can help you. Thank you for your support for this site!