java获取多线程的4种方法

java启动多线程的4种方法


继承Thread类

public class ThreadTest1 extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Thread is printing");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ThreadTest1 myThread = new ThreadTest1();
        myThread.start();
        for (int i = 0; i < 3; i++) {
            System.out.println("Main is printing");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

运行结果:

Main is printing
Thread is printing
Main is printing
Thread is printing
Thread is printing
Main is printing
Thread is printing
Thread is printing

实现Runnable接口

public class ThreadTest2 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Thread is printing");
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ThreadTest2 myThread = new ThreadTest2();
        Thread thread = new Thread(myThread);
        thread.start();
        for (int i = 0; i < 3; i++) {
            System.out.println("Main is printing");
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果

Main is printing
Thread is printing
Main is printing
Thread is printing
Main is printing
Thread is printing
Thread is printing
Thread is printing

实现Callable接口

这种方法可以返回线程执行的结果。

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadTest3 implements Callable<String> {

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 5; i++) {
            System.out.println("Thread is printing");
            Thread.currentThread().sleep(1000);
        }
        return "Done";
    }

    public static void main(String[] args) {
        ThreadTest3 myThread = new ThreadTest3();
        FutureTask<String> futureTask = new FutureTask<String>(myThread);
        Thread thread = new Thread(futureTask);
        thread.start();
        for (int i = 0; i < 3; i++) {
            System.out.println("Main is printing");
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            System.out.println("Thread return: " + futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

运行结果

Main is printing
Thread is printing
Main is printing
Thread is printing
Main is printing
Thread is printing
Thread is printing
Thread is printing
Thread return: Done

通过线程池获取线程

Java通过Executors提供四种线程池

  • CachedThreadPool 创建可缓存的线程池
  • FixedThreadPool 创建定长的线程池,超出线程池长度的线程在队列中等待
  • ScheduledThreadPool 创建一个定长的线程池,支持定时和周期性任务
  • SingleThreadExecutor 创建一个单线程的线程池,所有任务通过指定顺序(FIFO,LIFO,优先级)执行

这里就不详细介绍四种线程池的区别了。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTest4 {
    public static void main(String[] args) {
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            final int j = i;
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    for (int k = 0; k < 2; k++) {
                        System.out.printf("Thread %d is printing\n", j);
                        try {
                            Thread.currentThread().sleep(1000 );
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
    }
}

运行结果:

Thread 0 is printing
Thread 4 is printing
Thread 3 is printing
Thread 2 is printing
Thread 1 is printing
Thread 1 is printing
Thread 2 is printing
Thread 3 is printing
Thread 4 is printing
Thread 0 is printing
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值