Thread creation and startup
Inherit Thread class: override run method
1 package NiuMuThread; 2 3 public class MyThread extends Thread{ 4 5 @Override 6 public void run() { 7 // TODO Auto-generated method stub 8 for(int i=0;i<100;i++) { 9 // The name of the static method of the thread - the current thread 10 System.out.println(Thread.currentThread().getName()+":"+i); 11 try { 12 Thread.sleep(100); 13 } catch (InterruptedException e) { 14 // TODO Auto-generated catch block 15 e.printStackTrace(); 16 } 17 } 18 } 19 }
Implement the Runnable interface: override the run method
1 package NiuMuThread; 2 3 public class MyThread2 implements Runnable{ 4 5 @Override 6 public void run() { 7 // TODO Auto-generated method stub 8 for(int i=0;i<100;i++) { 9 // The name of the static method of the thread - the current thread 10 System.out.println(Thread.currentThread().getName()+":"+i); 11 try { 12 Thread.sleep(100); 13 } catch (InterruptedException e) { 14 // TODO Auto-generated catch block 15 e.printStackTrace(); 16 } 17 } 18 } 19 }
Test:
1 package NiuMuThread; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 //First thread 7 MyThread thread=new MyThread(); 8 //Third thread 9 MyThread2 r=new MyThread2(); 10 11 Thread th2=new Thread(r); 12 thread.start();//Start thread 13 th2.start(); 14 15 //Second thread 16 for(int i=0;i<100;i++) { 17 // The name of the static method of the thread - the current thread 18 System.out.println(Thread.currentThread().getName()+":"+i); 19 try { 20 Thread.sleep(100); 21 } catch (InterruptedException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 } 26 } 27 }
Note: by implementing the Runnable interface, you need new Thread(runnable) to create a thread object
***Call the start () method on the Thread object of the Thread instead of run () or other methods.
Some common problems
1. The name of a thread. A running thread always has a name. The name comes from two sources: the name given by the virtual machine itself or the name you set yourself. When the thread name is not specified, the virtual opportunity specifies the name for the thread. The name of the main thread is main, and the name of the non main thread is uncertain.
2. The name of each thread can be set or obtained.
3. The method to get the object of the current thread is: Thread.currentThread();
4. In the above code, it can only be guaranteed that each thread will start and each thread will run until it is completed. Just because a series of threads are started in a certain order does not mean that they will be executed in that order. For any set of started threads, the scheduler cannot guarantee their execution order and duration.
5. When the thread target run () method ends, the thread completes.
6. Once a thread starts, it can never restart. Only one new thread can be restarted, and only once. A runnable or dead thread can be restarted.
7. Thread scheduling is a part of the JVM. In fact, on a CPU machine, only one thread can be run at a time. Only one thread stack runs at a time. The JVM thread scheduler determines which thread is actually running.
One of the many runnable threads will be selected as the current thread. The order in which runnable threads are selected is not guaranteed.
8. Although queues are usually used, this is not guaranteed. Queue form means that when a thread completes a "round", it moves to the end of the runnable queue and waits until it finally queues to the front of the queue. In fact, we call it a runnable pool rather than a runnable queue to help understand that threads are not always arranged in a guaranteed order.
9. Although we do not have a thread scheduler that cannot be controlled, we can affect the thread scheduling in other ways.