There are two ways to establish threads in Java, namely inheriting the Thread class and implementing the Runnable interface.
Inherit Thread
public class MyThread extends Thread{ public MyThread(String name){ super(name); } int i; public void run(){ for(i=0;i<5;i++){ System.out.println(getName()+"--"+i); } } public static void main(String[] agrs){ new MyThread("Thread1").start(); new MyThread("Thread2").start(); } } /*Thread 1-0Thread 1-1Thread 1-2Thread 1-3Thread 1-4Thread 2-0Thread 2-1Thread 2-2Thread 2-3Thread 2-4*/As mentioned above, thread objects in java must be created in the form of a class, and the run() method of the base class must be overridden in this class. This method is actually the execution body of the thread. Calling the start method of this class instance implicitly calls the run method.
It is not difficult to see that since MyThread was new twice, the examples of the two times are different, that is, each has its own i variables, which are independent of each other.
Runnable interface
public class MyRunnable implements Runnable{ int i; public void run(){ for(i=0;i<50;i++){ System.out.println(Thread.currentThread().getName()+"--"+i);//This.getName() cannot be directly used } } public static void main(String[] agrs){ MyRunnable myRun=new MyRunnable(); new Thread(myRun,"Thread1").start(); new Thread(myRun,"Thread2").start(); } } /*Thread 1-0thread 1-1thread 2-0thread 2-3thread 2-4thread 2-5thread 2-6thread 2-7thread 2-8th thread 2-9th thread 2-10th thread 2-11th thread 2-12th thread 2-13thread 2-14th thread 2-15th thread 1-2th thread 2-16th thread 2-18th thread 2-19th thread 2-20th thread 2-21th thread 2-22th thread 2-23th thread 2-24th thread 1-17th thread 2--25 threads 1--26 threads 2--27 threads 1--28 threads 1--30 threads 2--29 threads 1--31 threads 2--32 threads 2--34 threads 2--35 threads 2--36 threads 2--37 threads 1--33 threads 2--38 threads 1--39 threads 1--41 threads 2--40 threads 1--42 threads 1--44 threads 1--45 threads 2--43 threads 1--46 threads 2--47 threads 2--49 threads 1--48*/It can be seen that since this method is to load an object into the Thread class as a target, even if there are many Thread objects in new, as long as the target is the same reference object, the run method of the object is called, and all threads share the resources of the target object. Therefore, you will see that Thread 1 and Thread 2 output a total of 51 times, and the two threads have completed the output from i from 0 to 49, and do not output 5 times respectively like above. As for why 51 are output, the two threads enter the ready state almost at the same time (the start method just lets the thread enter the ready state), it is not difficult to find that when i is equal to 0, thread 1 and thread 2 are both in the running state at the same time, resulting in concurrency phenomenon, and the output of i=0 together. After that, the CPU constantly switches threads, so that only one thread is output at the same time.
Thread status
Threads are divided into 4 states
Ready state: Call the start method and enter the ready state.
Running state: The thread in the ready state will be scheduled by jvm and become the running state.
Blocking state: If some synchronization methods do not return results, a blocking state occurs, or sleep and yeild.
Death state: The method body is executed or a thread is forced to stop.
Basic operations of threads
join() merge thread: After the current thread calls the join method of a thread, it will wait for the thread to complete execution before the thread will continue.
sleep(long milliseconds) thread sleep: blocks the current thread, and it will continue only when the blocking time is up. When blocking again, the CPU ownership will be handed over to other threads, so sleep(1) is often used to switch threads.
**yield() thread concessions: **yeild is similar to sleep, but it will only concessions to other threads at higher level or at the same level. If no other threads are lower than their level, the thread will be executed again.
Background thread
After a program is executed by the operating system, there will be a process, and a process has at least one thread (main thread). The main thread does not have much special features than other threads, just because it is the earliest thread to be executed, other threads will be created in the main thread. If not specified, the foreground thread (including the main thread) is created by default. If setDaemon(true) is called, the thread is explicitly set as a background thread. The background thread is a Daemon thread. As you can see from the name, its main function is to provide guardian and service functions for other threads. When all foreground threads end, the background thread will be forced to end because it has no meaning to exist at this time.
Foreground thread
public class ForeAndBackThread extends Thread{ public ForeAndBackThread(String name){ super(name); } public void run(){ int i; for(i=0;i<9999;i++){ System.out.println(this.getName()+"--"+i); } } public static void main(String[] args){ ForeAndBackThread th=new ForeAndBackThread("Thread A"); //th.setDaemon(true); th.start(); int j; for(j=0;j<3;j++){ System.out.println(Thread.currentThread().getName()+"--"+j); } } }The complete output child thread of the program is 0 to 9998; it means that the main thread is not special, and its end will not affect the operation of other foreground threads.
Background thread
public class ForeAndBackThread extends Thread{ public ForeAndBackThread(String name){ super(name); } public void run(){ int i; for(i=0;i<9999;i++){ System.out.println(this.getName()+"--"+i); } } public static void main(String[] args){ ForeAndBackThread th=new ForeAndBackThread("Thread A"); th.setDaemon(true); th.start(); int j; for(j=0;j<3;j++){ System.out.println(Thread.currentThread().getName()+"--"+j); } } }The program cannot output 0-9998 in full and exits, indicating that after the main thread of the foreground ends, jvm forces the background thread to end.
Summarize
The above is all about the analysis of basic examples of Java threads in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!