创建多线程的四种方法

方法一:继承Thread类创建线程
public class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i<= 20; i++) {
            if(i%2 == 0){
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
    public static void main(String[] args) {
        MyThread myThread1 = new MyThread();
        MyThread myThread2 = new MyThread();
        myThread1.setName("线程1");
        myThread2.setName("线程2");
        myThread1.start();
        myThread2.start();
    }
}
方法二:实现Runnable接口创建线程
public  class MyThread implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i<= 20; i++) {
            if(i%2 == 0){
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }

    public static void main(String[] args) {
        MyThread myThread1 = new MyThread();
        MyThread myThread2 = new MyThread();
        Thread thread1 = new Thread(myThread1);
        Thread thread2 = new Thread(myThread2);
        thread1.setName("线程1");
        thread2.setName("线程2");
        thread1.start();
        thread2.start();
}
方法三:实现Callable接口创建线程
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class NumThread implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i<= 20; i++) {
            if(i%2 == 0){
                System.out.println(Thread.currentThread().getName() + ":" + i);
                sum += i;
            }
        }
        return sum;
    }
}
public class MyThread {
    public static void main(String[] args) {
        NumThread numThread = new NumThread();
        FutureTask<Integer> futureTask = new FutureTask(numThread);
        new Thread(futureTask).start();
        try {
            Integer sum = futureTask.get();
            System.out.println("整数和为:" + sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
方法四:使用线程池创建线程
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class NumThread implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i<= 20; i++) {
            if(i%2 == 0){
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}
public class MyThread {
    public static void main(String[] args) {
         //创建一个可根据需要创建新线程的线程池
        ExecutorService service = Executors.newCachedThreadPool();
        //创建一个可重用固定线程数量的线程池
//        ExecutorService service = Executors.newFixedThreadPool(10);
        //创建一个只有一个线程的线程池
//        ExecutorService service = Executors.newSingleThreadExecutor();
        for (int i = 0; i<10; i++){
            service.execute(new NumThread());
        }
        service.shutdown();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值