多线程总结

多线程的创建

继承Thread重写run方法

package 多线程;

public class Thread1 extends Thread{
    @Override
    public void run() {
        System.out.println(this.getClass());
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        Thread1 thread = new Thread1();
        thread.start();

    }
}

实现runnable接口并传入Thread

package 多线程;

public class Thread2 implements Runnable{
    @Override
    public void run() {
        System.out.println(super.getClass());
        System.out.println(this.getClass().getClassLoader());
    }

    public static void main(String[] args) {
        Thread2 thread2 = new Thread2();
        Thread thread = new Thread(thread2);
        thread.start();
    }
}

实现callable接口传入FutureTask再传入Thread

package 多线程;

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

public class Thread3 implements Callable {
    @Override
    public Object call() throws Exception {
        return this.getClass().toString();
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread3 thread3 = new Thread3();
        FutureTask<Object> ft = new FutureTask<>(thread3);
        new Thread(ft).start();
        System.out.println(ft.get());
    }
}


newFixedThreadPool与newSingleThreadExecutor

public static void main(String args[]) {
        ExecutorService fixPool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            fixPool.execute(getThread(i));
        }
        fixPool.shutdown();
        ExecutorService singPool = Executors.newSingleThreadExecutor();
        for (int i=0;i<10;i++){
//            singPool.execute(getThread(i));
            singPool.submit(getThread(i));
        }
        singPool.shutdown();
    }

newScheduledThreadPool

public static void main(String args[]) {

        ScheduledExecutorService ses = Executors.newScheduledThreadPool(10);
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getId() + "执行了");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 0, 2, TimeUnit.SECONDS);
    }

ThreadPoolExecutor

public static void main(String[] args){
        MyTask1 task = new MyTask1();
        ExecutorService es = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MICROSECONDS, new SynchronousQueue<Runnable>(), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                System.out.println("创建线程"+t);
                return  t;
            }
        });
        for (int i = 0;i<=4;i++){
//            es.submit(task);
            es.execute(task);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值