Thread class
The thread class exists at JDK1.0, and multithreaded development that comes into contact with Java basically starts with this class.
Thread-Defined threading class
The way to create a thread using thread is simple, thread is a class, and when you need to create a thread, we just need to inherit the class and rewrite the run () method.
class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } @Override public void run() { System.out.println(Thread.currentThread().getName()+" begin"); try { Thread.sleep(500); System.out.println(minPrime); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" end"); }}
The Primethread class above inherits the thread class, so you can implement an open thread with that class.
Thread's Open threads
Here's how to open a thread:
public static void main(String[] args) { System.out.println("Thread begin:" + Thread.currentThread().getName()); PrimeThread primeThread = new PrimeThread(100L); primeThread.start(); System.out.println("Thread end:" + Thread.currentThread().getName());}
When called, new takes an object of the Primethread class, and then calls the Start () method (note that the run () method is not called). The final results are as follows
Creating a simple multithreading is so simple in Java, but because Java is single-inheritance, inheriting the Thread class is a very cost-effective approach. In JDK1.0, there is another way to compensate for this, which is the way to implement the Runnable interface.
Runnable interface Runnable definition Thread class
At this point, the way to create a multithreaded class using the Runnable interface is to implement Runnable, and then rewrite the run () method, as follows:
public class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { System.out.println(Thread.currentThread().getName() + " Run begin"); try { Thread.sleep(500); System.out.println(minPrime); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " Run end"); }}
Runnable's Open thread
It's basically similar to inheriting the Thread class, but we don't have to inherit it. The invocation is still going through the Thread class, called the following way:
public static void main(String[] args) { System.out.println(Thread.currentThread().getName()+" begin"); PrimeRun primeRun = new PrimeRun(100L);// new new Thread(primeRun).start(); // 调用 System.out.println(Thread.currentThread().getName()+" end");}
The operation results are as follows
Can be found, in fact, the two ways to implement multithreading are essentially the same. Finally, the thread is opened through the start () method in the thread class, which notifies the thread planner that this thread is ready and the system can schedule time to invoke the run () method in the thread class. Therefore, if no priority is assigned, the order in which the threads are executed is performed according to the scheduling algorithm of the system, which is unordered.
The above is the most basic Java open multithreading method is introduced, the next few will be more in-depth introduction of several methods and principles.