Home > Java > javaTutorial > body text

Example analysis of how Java creates threads

PHPz
Release: 2023-05-13 16:52:06
forward
1064 people have browsed it

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
*/
Copy after login

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
Copy after login

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();
    }
}
Copy after login

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);
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!