线程创建的三种方式

线程创建的三种方式

1、Callable

2、Thread

3、Runable

 

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Demo6_Callable {

	/**
	 * @param args
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService pool = Executors.newFixedThreadPool(2);//创建线程池
		Future<Integer> f1 = pool.submit(new MyCallable(100));				//将线程放进池子里并执行
		Future<Integer> f2 = pool.submit(new MyCallable(50));
		
		System.out.println(f1.get());
		System.out.println(f2.get());
		
		pool.shutdown();							//关闭线程池
	}

}

class MyCallable implements Callable<Integer> {
	private int num;
	public MyCallable(int num) {
		this.num = num;
	}
	@Override
	public Integer call() throws Exception {
		int sum = 0;
		for(int i = 1; i <= num; i++) {
			sum += i;
		}
		
		return sum;
	}
	
}

public class Demo2_Thread {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MyThread mt = new MyThread();		//4,创建Thread类的子类对象
		mt.start();							//5,开启线程
		
		for(int i = 0; i < 10; i++) {
			System.out.println("bb");
		}
	}

}

class MyThread extends Thread {				//1,继承Thread
	public void run() {						//2,重写run方法
		for(int i = 0; i < 10; i++) {		//3,将要执行的代码写在run方法中
			System.out.println("aaaaaaaaaaaa");
		}
	}
}

 


 

public class Demo3_Thread {

   /**
    * @param args
    */
   public static void main(String[] args) {
      MyRunnable mr = new MyRunnable();  //4,创建Runnable的子类对象
      //Runnable target = mr;    mr = 0x0011
      Thread t = new Thread(mr);       //5,将其当作参数传递给Thread的构造函数
      t.start();                   //6,开启线程
      
      for(int i = 0; i < 1000; i++) {
         System.out.println("bb");
      }
   }

}

class MyRunnable implements Runnable {    //1,定义一个类实现Runnable

   @Override
   public void run() {                   //2,重写run方法
      for(int i = 0; i < 1000; i++) {       //3,将要执行的代码写在run方法中
         System.out.println("aaaaaaaaaaaa");
      }
   }
   
}
public class Demo4_Thread {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Thread() {										//1,继承Thread类
			public void run() {								//2,重写run方法
				for(int i = 0; i < 1000; i++) {				//3,将要执行的代码写在run方法中
					System.out.println("aaaaaaaaaaaaaa");
				}
			}
		}.start();											//4,开启线程
		
		new Thread(new Runnable() {							//1,将Runnable的子类对象传递给Thread的构造方法
			public void run() {								//2,重写run方法
				for(int i = 0; i < 1000; i++) {				//3,将要执行的代码写在run方法中
					System.out.println("bb");
				}
			}
		}).start();											//4,开启线程
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值