#### Method 1: Inherit Thread class as Thread object (inherit Thread object)
Public class CreatThreadDemo1 extends Thread{/** * constructor: extends Thread(String name); @param */ public CreatThreadDemo1(String name){super(name); } @Override public voidrun() {
while(! interrupted()){ System.out.println(getName()+"The thread executed...");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
CreatThreadDemo1 d1 = new CreatThreadDemo1("first");
CreatThreadDemo1 d2 = new CreatThreadDemo1("second"); d1.start(); d2.start(); d1.interrupt(); // Interrupt the first thread}}Copy the code
The normal method, which I won’t talk too much about, the interrupted method, is to see if the thread has been interrupted. A stop method is not allowed to terminate a thread. The stop method does not release occupied resources. So when we design our programs, we should design with the thought of interrupting threads in mind, just like the code above. ###### ways to make threads wait
- Thread.sleep(200); // Thread rest 2ms
- Object. The wait (); // Let the thread wait until it stops sleeping when notify or notifyAll of Object is called
#### Method 2: Implement the Runnable interface as a thread task
public class CreatThreadDemo2 implements Runnable {
@Override
public void run() {
while (true){
System.out.println("The thread executed..."); }} public static void main(String[] args) {Thread Thread = new Thread(new CreatThreadDemo2()); // Start the thread thread.start(); }}Copy the code
Runnable is only used to modify the task performed by the thread; it is not a thread object. To start a Runnable object, you must place it in a thread object.
#### Method three: Anonymous inner class creates thread objects
Public class CreatThreadDemo3 extends Thread{public static void main(String[] args) {// Create Thread object newThread(){
@Override
public void run() {
System.out.println("The thread executed..."); } }.start(); // Create Thread object new Thread(new) with Thread taskRunnable() {
@Override
public void run() {
System.out.println("The thread executed..."); } }).start(); // Create Thread object new Thread(new) with Thread task and override run methodRunnable() {
@Override
public void run() {
System.out.println("The runnable run thread executed...");
}
}){
@Override
public void run() {
System.out.println("Override run thread executes..."); } }.start(); }}Copy the code
A Thread object that creates a Thread task and overrides the run method runs only the run method of Thread. Let’s look at the source code for Thread.
#### Method 4: Create a thread with a return value
public class CreatThreadDemo4 implements Callable { public static void main(String[] args) throws ExecutionException, InterruptedException { CreatThreadDemo4 demo4 = new CreatThreadDemo4(); FutureTask<Integer> task = new FutureTask<Integer>(demo4); //FutureTask implements the runnable interface Thread Thread = new Thread(task); thread.start(); System.out.println("I can do some other business logic here... Because FutureTask is doing tasks ahead of time."); Integer result = task.get(); System.out.println(The result of the operation in the thread is:+result); @override public Object call() throws Exception {int result = 1; System.out.println("In business logic calculation...");
Thread.sleep(3000);
returnresult; }}Copy the code
Callable interface introduction:
public interface Callable<V> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}
Copy the code
Returns the call method of the specified generic type. The FutureTask’s GET method is then called to get the return value of the Call method.
#### Method 5: Timer Timer
public class CreatThreadDemo5 {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Timer thread executed..."); }}, 0100, 0); // Delay 0, period 1s}}Copy the code
#### Method 6: Create a thread from a thread pool
ExecutorService threadPool = public class CreatThreadDemo6 {public static void main(String[] args) {// create a threadPool with 10 threads Executors.newFixedThreadPool(10); long threadpoolUseTime = System.currentTimeMillis();for(int i = 0; i<10; i++){ threadPool.execute(newRunnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"The thread executed..."); }}); } long threadpoolUseTime1 = System.currentTimeMillis(); System.out.println("Multithreaded time"+(threadpoolUseTime1-threadpoolUseTime)); // destroy the threadPool threadpool.shutdown (); threadpoolUseTime = System.currentTimeMillis(); }}Copy the code
#### method 7: Using java8 stream to implement concurrent lambda expressions https://www.jianshu.com/p/3a08dc78a05f java8-stream:https://www.jianshu.com/p/ea16d6712a00
public class CreatThreadDemo7 { public static void main(String[] args) { List<Integer> values = ,20,30,40 arrays.aslist (10); Int result = values.parallelstream ().mapToint (p -> p*2).sum(); System.out.println(result); Value.parallelstream ().foreach (p-> system.out.println (p)); value.parallelstream (p-> system.out.println (p)); }}Copy the code
200, 40, 10, 20, 30Copy the code
How do you prove that it’s concurrent? They’re not printed in order.