线程创建方式

import lombok.SneakyThrows;

import java.util.concurrent.*;

public class T {
    @SneakyThrows
    public static void main(String[] args) {
        myRunnableTest_start();
    }

    @SneakyThrows
    public static void myCallableTest() {
        FutureTask<Boolean> futureTask = new FutureTask<>(new MyCallable());
        new Thread(futureTask).start();
        System.out.println(futureTask.get());
    }

    /*
    IllegalThreadStateException异常
    * */
    public static void myRunnableTest_start() {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        //strat方法只能调用一次
        thread.start();
        thread.start();
    }

    public static void myRunnableTest_run() {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        //普通方法调用
        thread.run();
        thread.run();
    }
}

class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("执行MyRunnable run()");
    }
}

class MyThread extends Thread {
    @Override
    public void run() {

    }
}

class MyCallable implements Callable<Boolean> {

    @Override
    public Boolean call() throws Exception {
        return false;
    }
}

class MyExecutor {
    public void myRun() {
        ExecutorService service = Executors.newFixedThreadPool(10);
        service.submit(() -> {
        });
        service.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return null;
            }
        });
    }
}





posted @ 2024-08-14 10:22  干饭达人GoodLucy  阅读(2)  评论(0编辑  收藏  举报