Threads in Java (I) (basic method)

Keywords: jvm

1, The concept of multithreading

1. Process: every program being executed is called a process. Process is an independent unit of resource allocation and call. Each process has its own memory space and system resources.

2. Thread: a thread is an execution path of a process. A process with only one execution path is called a single threaded program. If a process has multiple execution paths, it is called a multithreaded program.

 

2, Implementation mode of multithreading (two kinds)

1. Inherit Thread class and override run() method

Start: create subclass object. start()

  1. public class MyThread extends Thread {//Inherit Thread class  
  2.       
  3.     @Override  
  4.     public void run() {  
  5.         for(int i=1;i<101;i++) {  
  6.             System.out.println(getName()+"=="+i);  
  7.         }  
  8.     }  
  9. }  
  1. public class ComeTrue {  
  2.       
  3.     public static void main(String[] args) {  
  4.           
  5.         MyThread t1 =new MyThread(); //Create a subclass object  
  6.         MyThread t2 =new MyThread();  
  7.           
  8.         t1.start(); //Start thread  
  9.         t2.start();  
  10.     }  
  11. }  

2. Implement the runnable interface and rewrite the run() method

Starting: using static agents

1) create a real role

2) create an agent role

3). Agent role. start()

  1. public class MyThread implements Runnable{//Implement the Runnable interface  
  2.       
  3.     @Override  
  4.     public void run() {  
  5.         for(int i=1;i<101;i++) {  
  6.             System.out.println(Thread.currentThread().getName()+"=="+i);  
  7.         }  
  8.     }  
  9. }  

  1. public class ComeTrue {  
  2.       
  3.     public static void main(String[] args) {  
  4.           
  5.         MyThread my =new MyThread(); //Creating real objects  
  6.           
  7.         Thread t1 = new Thread(my,"Thread one");  //Create proxy object  
  8.         Thread t2 = new Thread(my,"Thread two");  //Create proxy object  
  9.           
  10.         t1.start(); //Start thread  
  11.         t2.start();  
  12.     }  
  13. }  

3, Thread life cycle

4, Common methods and thread control

1. Common methods

  1. public class MyThread implements Runnable{//Implement the Runnable interface  
  2.       
  3.     @Override  
  4.     public void run() {  
  5.         for(int i=1;i<101;i++) {  
  6.             System.out.println(Thread.currentThread().getName()+"=="+i);  
  7.         }  
  8.     }  
  9. }  
  1. public class ComeTrue {  
  2.       
  3.     public static void main(String[] args) {  
  4.           
  5.         MyThread my =new MyThread(); //Creating real objects  
  6.           
  7.         Thread t1 = new Thread(my,"Thread one");  //Create proxy object  
  8.           
  9.         t1.start(); //Start thread  
  10.         /* 
  11.          * common method 
  12.          */  
  13.         t1.isAlive();           //Determine whether the thread is still alive, that is, whether the thread has not been terminated  
  14.         t1.setPriority(10);             //Set the priority value of the thread to 1-10  
  15.         t1.getPriority();       //Get priority value of thread  
  16.         t1.setName("Thread one");          //Set the name of the thread  
  17.         t1.getName();           //Get the name of the thread  
  18.         t1.currentThread();     //Get the currently running thread object, that is, get itself  
  19.     }  
  20. }  

2. Thread control

  1. public class MyThread implements Runnable{//Implement the Runnable interface  
  2.       
  3.     @Override  
  4.     public void run() {  
  5.         for(int i=1;i<101;i++) {  
  6.             try {  
  7.                 Thread.sleep(1000);  //Thread sleep time association in thread blocking state  
  8.             } catch (InterruptedException e) {  
  9.                 // TODO Auto-generated catch block  
  10.                 e.printStackTrace();  
  11.             }  
  12.             System.out.println(Thread.currentThread().getName()+"=="+i);  
  13.         }  
  14.     }  
  15. }  
  1. public class ComeTrue {  
  2.       
  3.     public static void main(String[] args) {  
  4.           
  5.         MyThread my =new MyThread(); //Creating real objects  
  6.           
  7.         Thread t1 = new Thread(my,"Thread one");  //Create proxy object  
  8.         Thread t2 = new Thread(my,"Thread two");  //Create proxy object  
  9.           
  10.         //Background thread  
  11.         //Set background (guard) thread - it needs to be set before the thread starts  
  12.         //Daemons: when the last non daemons end, the daemons end their work with the JVM, such as garbage collector  
  13.         t1.setDaemon(true);  
  14.           
  15.         t1.start(); //Start thread  
  16.         t2.start(); //Start thread  
  17.           
  18.         /* 
  19.          * Thread control 
  20.          */  
  21.           
  22.         //Thread join  
  23.         try {  
  24.             t2.join(); //Thread join. Wait until the join thread is set to end, other threads will start running. Other threads are blocked  
  25.         } catch (InterruptedException e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.           
  29.         //Thread comity  
  30.         t2.yield(); //When t2 grabs the CPU time slice, it gives up once and scrambles again to be blocked  
  31.           
  32.         //stop(): thread termination thread death  
  33.         //interrupt(): thread interrupt thread block  
  34.     }  
  35. }  

Posted by largo on Thu, 02 Apr 2020 22:59:25 -0700