面试必备-线程创建方法

线程创建方法

  • (1)继承Thread类创建线程

  • (2)实现Runnable接口创建线程

  • (3)使用Callable和Future创建线程

  • (4)使用线程池例如用Executor框架

方法一:继承Thread类创建线程

public class MyThread1 extends Thread{
  public void run(){
		System.out.println(Thread.currentThread().getName() + "线程被调用");  
  }
}
public class ThreadDemo {
  public static void main(String[] args){
    new MyThread1().start();
  }
}

方法二:实现Runnable接口创建线程

class MyThread2 implements Runnable {
    @Override
    public void run() {
       System.out.println(Thread.currentThread().getName() + "线程被调用");  
    }
}
public class ThreadDemo {
  public static void main(String[] args){
    new Thread(new MyThread2()).start();
  }
}

方法三:实现Runnable接口创建线程

class MyThread3 implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("线程名:"+Thread.currentThread().getName());
        return Thread.currentThread().getName()+"运行结束";
    }
}
public class ThreadDemo {
  public static void main(String[] args){
     Thread.currentThread().setName("线程一");
        System.out.println("线程名:"+Thread.currentThread().getName());
        //创建FutureTask的对象
        FutureTask<String> task = new FutureTask<String>(new MyThread3());
        //创建Thread类的对象
        Thread thread3 = new Thread(task);
        thread3.setName("线程二");
        //开启线程
        thread3.start();
        //获取call()方法的返回值,即线程运行结束后的返回值
        System.out.println(task.get());
  }
}

方法四:使用线程池创建

class MyThread4  implements Runnable {
    @Override   
    public void run(){   
        System.out.println(Thread.currentThread().getName() + "线程被调用");  
        try{   
            Thread.sleep(1000);   
        }catch(InterruptedException e){   
            e.printStackTrace();   
        }   
    } 
}
public class ThreadDemo {
  public static void main(String[] args){
  	//等待队列
     BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<Runnable>(20);   
     //创建线程池,池中保存的线程数为2,允许的最大线程数为5 
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2,5,50,TimeUnit.MILLISECONDS,bqueue);  
        //创建多个任务
        Runnable r1 = new MyThread4();   
        Runnable r2 = new MyThread4();   
        Runnable r3 = new MyThread4();   
        Runnable r4 = new MyThread4();   
        Runnable r5 = new MyThread4();   
        Runnable r6 = new MyThread4();   
        Runnable r7 = new MyThread4();   
        Runnable r8 = new MyThread4();   
        Runnable r9 = new MyThread4();   
        Runnable r10 = new MyThread4();   
        //每个任务会在一个线程上执行  
        pool.execute(r1);   
        pool.execute(r2);   
        pool.execute(r3);   
        pool.execute(r4);   
        pool.execute(r5);   
        pool.execute(r6);   
        pool.execute(r7);   
        pool.execute(r8);  
        pool.execute(r9);   
        pool.execute(r10);   
        //关闭线程池   
        pool.shutdown(); 
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值