Three implementation methods of multithreading

Three implementation methods of threads

1. Thread

Multi-threading is achieved by inheriting the Thread class.

Inherit the Thread class and override the run method. Create an object of this class, and then call start()

package com.Unit1;

public class ThreadTest01 extends Thread{
    
    

    @Override
    public void run() {
    
    
        for (int i = 0; i < 10000; i++) {
    
    
            System.out.println(Thread.currentThread().getName() + i);

        }
    }

    public static void main(String[] args) {
    
    
        ThreadTest01 test = new ThreadTest01();
        test.start();
        for (int i = 0; i < 10000; i++) {
    
    
            System.out.println(Thread.currentThread().getName() + i);
        }
    }
}

2. Runnable

By implementing the Runnable interface to achieve

package com.Unit1;

public class RunTest01 implements Runnable {
    
    
    //设置胜者
    public static String winner;
    //设置长度
    public static int length = 100;

    @Override
    public void run() {
    
    
        //赛跑开始
        for (int i = 0; i <= length; i++) {
    
    
            System.out.println(Thread.currentThread().getName() + "已经跑了 " + i + "米");
            //判断是否完成。
            boolean flag = GameOver(i);
            if (flag) break;
        }
    }

    public boolean GameOver(int number) {
    
    
        if (winner != null) {
    
    
            return true;
        } else if (number >= length) {
    
    
            winner = Thread.currentThread().getName();
            System.out.println("胜者是:"+winner);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
    
    
        RunTest01 number = new RunTest01();
        new Thread(number,"1号").start();
        new Thread(number,"2号").start();
        new Thread(number,"3号").start();
        new Thread(number,"4号").start();
        new Thread(number,"5号").start();
        new Thread(number,"6号").start();
        new Thread(number,"7号").start();
    }

}

3. Callable

This is achieved by implementing the Callable interface, which accepts a return value. You also need to reference a FutureTask to transfer objects.

package com.Unit1;

import java.util.concurrent.*;

public class TestCallable implements Callable<Boolean> {
    
    
    private int ticket = 1;
    @Override
    public Boolean call() throws Exception {
    
    
        while(ticket <= 100 ){
    
    
            System.out.println("第 " + ticket+"张票被卖了");
            ticket++;
        }
        return true;
    }


    public static void main(String[] args) throws Exception{
    
    
        TestCallable tc = new TestCallable();
        //创建一个FutureTask实例,并传入tc对象
        FutureTask<Boolean> ft = new FutureTask<>(tc);
        new Thread(ft).start();
        //利用ft的get得到返回值。记得向上转型。
        boolean flag = (boolean) ft.get();
        System.out.println(flag);
    }
}

Guess you like

Origin blog.csdn.net/qq_45022687/article/details/119428481