This article explains Java's threading technology in more detail with examples. I believe it will be helpful for a deep understanding of Java programming. The details are as follows:
Many people have a certain understanding of threads when learning JAVA, and when we started to contact Android development, we really discovered how important threads are. This article will share with you the experience of using Java threads. For your reference.
First of all, everyone must understand that threads and processes are not the same thing. What is a process? The process is just like we need to execute the class file, and the thread is what actually calls the CPU resources to run. A class file generally has only one process, but there can be many threads. The execution of threads is an asynchronous execution method.
1. How to start another thread in the main function:
The sample code is as follows:
public class Thread_one { public static void main(String [] args){ Run run = new Run(); //run.run();//This is a method call, which is very different from a thread Thread thread = new Thread (run); thread.start();//Start the thread and call the thread's run() method for(int i=1; i<=20; i++){ System.out.println("The value of main thread i: --------"+i); } }}class Run implements Runnable{ @Override public void run() { for(int i=1; i<=20; i++){ System.out.println( "The value of sub-thread i: "+i); } }}
2. The sleep method in threads
The sample code is as follows:
public class Thread_sleep { /* * Thread pause*/ public static void main(String [] args){ Runone run = new Runone(); Thread thread = new Thread(run); thread.start(); try { Thread.sleep (5000); thread.interrupt();//Interrupt the execution of the thread//thread.stop();//Compared to interrupting the thread, stop is too rough and is not recommended. This method is generally used when the child thread needs to be forced to close. } catch (InterruptedException e) { e.printStackTrace(); } }}class Runone implements Runnable{ @Override public void run() { for(int i=1; i<10; i++){ try { Thread.sleep(1000 ); System.out.println("-----"+new Date()+"-----"); } catch (InterruptedException e) { return ;//When it is caught that the child thread is interrupted, Close the child thread directly} } }}
A special note here is that thread.interrupt(); can interrupt the execution of a thread. It is a little gentler than stop, but it is not the best way to close a thread. Here is a way for you:
public class Thread_stop { public static void main(String [] args){ Runthree run = new Runthree(); Thread th = new Thread(run); th.start(); try { Thread.sleep(5000); } catch ( InterruptedException e) { e.printStackTrace(); } run.setStop(); }}class Runthree implements Runnable{ boolean flag; @Override public void run() { flag = true; int i = 0; while(flag){ try { System.out.println("child thread----"+(i++)); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void setStop(){ flag = false; } }
Let me briefly introduce to you the merging and giving away in threads:
1. How to merge threads , the join() method is called here
The sample code is as follows:
public class Thread_join { /* * Merge threads*/ public static void main(String [] args){ Runtwo run = new Runtwo(); Thread thread = new Thread(run); thread.start(); try { thread.join ();//Merge threads, this is equivalent to method call} catch (InterruptedException e) { e.printStackTrace(); } for(int i=0; i<10; i++){ System.out.println("main Thread: "+i); } }}class Runtwo implements Runnable{ @Override public void run() { for(int i=0; i<10; i++){ System.out.println("Child thread: --- -"+i); } } }
2. How to give up the thread . The yield() method of Thread is called here, as shown below:
public class Thread_yield { /**Give up CPU * @param args */ public static void main(String[] args) { Th th = new Th("aaa"); th.start(); for(int i = 0 ; i<=10; i++){ System.out.println("Main thread----"+i); } }}class Th extends Thread{ Th(){} Th(String s){super(s) ;} @Override public void run() { for(int i = 0; i<=10; i++){ if(i%3!=0){ System.out.println("child thread"+i); } else{ System.out.println("Sub-thread i="+i+" thread switches"); yield();//This method can only be used if you inherit from Thread} } }}
Finally, I would like to share with you the issue of thread priority . The code is as follows:
public class Thread_priority { /* * priority sets the priority of the thread * The default priority of Thread is 5; the maximum priority of Thread is 10 and the minimum is 0 */ public static void main(String [] args){ T1 t1 = new T1(); T2 t2 = new T2(); t1.start(); //t1.setPriority(Thread.NORM_PRIORITY+3); //Set the priority of t1 t2.start(); }}class T1 extends Thread { @Override public void run() { for(int i = 0; i<50; i++){ System.out.println("Thread T1-----"+i); } }}class T2 extends Thread{ @Override public void run() { for(int i = 0; i<50; i++){ System.out.println("Thread T2"+i); } } }
I believe that you have basically understood the threading mechanism in JAVA through the above code. I hope that what this article describes will be helpful to you in your in-depth study of Java programming.