Home  >  Article  >  Java  >  Example analysis of how Java creates threads

Example analysis of how Java creates threads

PHPz
PHPzforward
2023-05-13 16:52:061022browse

Inherit Thread, use anonymous inner class here

@Slf4j(topic = "c.Test1")
public class Test1 {
    public static void main(String[] args) {
        //创建线程对象
        Thread t = new Thread(){
            @Override
            public void run() {
                //要执行的任务
                log.debug("running");
            }
        };
        //设置线程的名字
        t.setName("t1");
        //启动线程
        t.start();
        log.debug("running");
    }
}
/*
19:44:31.998 [main] DEBUG c.Test1 - running
19:44:31.998 [t1] DEBUG c.Test1 - running
*/

implement Runnable interface, cooperate with Thread class, also use anonymous inner class

  • separate threads and tasks

  • Thread represents the thread

  • Runnable represents the runnable task

@Slf4j(topic = "c.Test2")
public class Test2 {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //要执行的任务
                log.debug("running");
            }
        };
        //创建线程对象
        Thread t = new Thread(runnable, "t2");
        //启动线程
        t.start();
    }
}
//19:52:27.646 [t2] DEBUG c.Test2 - running

In javajava, there is @ The FunctionalInterface@FunctionalInterface annotation means that the interface has only one abstract method, which can be simplified by lambdalambda expression

@Slf4j(topic = "c.Test2")
public class Test2 {
    public static void main(String[] args) {
        Runnable runnable = () -> {
            //要执行的任务
            log.debug("running");
        };
        //创建线程对象
        Thread t = new Thread(runnable, "t2");
        //启动线程
        t.start();
    }
}

FutureTask with Thread

Because FutureTask can interface a Callable type parameter, used Handling situations with return values

@Slf4j(topic = "c.Test3")
public class Test3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建任务对象
        FutureTask<Integer> task = new FutureTask<>(() -> {
            log.debug("running");
            Thread.sleep(1000);
            return 100;
        });
        /*
        用lambda化简前
         */
        FutureTask<Integer> task1 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.debug("running");
                Thread.sleep(1000);
                return 100;
            }
        });
        //参数1是任务的对象, 参数2是线程的名字
        Thread t = new Thread(task, "t3");
        t.run();
        //主线程堵塞,同步等待task执行完毕的结果
        Integer integer = task.get();
        log.debug("结果是:{}", integer);
    }
}

The above is the detailed content of Example analysis of how Java creates threads. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete