Detailed usage of JAVA threads

  • 2020-04-01 03:26:48
  • OfStack

This article with the example of a more detailed explanation of Java threading technology, I believe that the in-depth understanding of Java programming has a certain help. The details are as follows:

Many people have a certain understanding of threads when learning JAVA, but when we started to touch Android development, we really discovered how important threads are. This article will share the usage of JAVA threads with you for your reference.

First of all, you have to make sure that a thread is not the same thing as a process. What is a process? The process is like we need to execute the class file, while the thread is actually calling the CPU resource to run. A class file typically has only one process, but threads can have many, and thread execution is asynchronous.

I. how to open 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, and 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 main thread i the   Value: --------"+i);
    }
  }
}
class Run implements Runnable{

  @Override
  public void run() {
    for(int i=1; i<=20; i++){
      System.out.println(" The child thread i the   Value: "+i);
    }
  }
}

The sleep method in the thread

The sample code is as follows:


public class Thread_sleep {
  
  public static void main(String [] args){
    Runone run = new Runone();
    Thread thread = new Thread(run); 
    thread.start();
    try {
      Thread.sleep(5000);
      thread.interrupt();//Interrupts thread execution
      //thread.stop();// Relative to the interrupt thread, stop It is not recommended and is generally used when a 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 the captured child thread is interrupted, the child thread is closed directly
      }
    }
  }
}

In particular, thread.interrupt(); Can interrupt the execution of the thread, relative to stop gentle so a little bit, but it is not the best way to close the thread, here is a way for you to:


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(" The child thread ----"+(i++));
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
  public void setStop(){
    flag = false;
  }  
}

The following is a brief introduction to the thread in the merge and give:

How to merge threads , the join() method is called

The sample code is as follows:


public class Thread_join {
  
  public static void main(String [] args){
    Runtwo run = new Runtwo();
    Thread thread = new Thread(run);
    thread.start();
    try {
      thread.join();//Merge threads, which is the equivalent of a method call
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    for(int i=0; i<10; i++){
      System.out.println(" The main thread: "+i);
    }
  }
}
class Runtwo implements Runnable{

  @Override
  public void run() {
    for(int i=0; i<10; i++){
      System.out.println(" The child thread: ----"+i);
    }
  }  
}

Two, how to give out the thread , the yield() method of Thread is called, as follows:


public class Thread_yield {

  
  public static void main(String[] args) {
    Th th = new Th("aaa");
    th.start();
    for(int i = 0 ; i<=10; i++){
      System.out.println(" The 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(" The child thread "+i);
      }else{
        System.out.println(" The child thread i="+i+"  Thread switching ");
        yield();//You can use this method by inheriting from Thread
      }
    }
  }
}

Finally, I would like to share with you about The priority of threads , the code is as follows:


public class Thread_priority {
  
  public static void main(String [] args){
    T1 t1 = new T1();
    T2 t2 = new  T2();
    t1.start();  
    //t1.setPriority(Thread.NORM_PRIORITY+3);// Set up the t1 The priority of the 
    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 basic understanding of the threading mechanism in JAVA through the above code, I hope that this article is helpful for you to further study JAVA programming.


Related articles: