线程的4种创建方式

目录

线程的4种创建方式:

第一种:

第二种:

第三种:

第四种:


线程的4种创建方式:

第一种:

public class A1 {
public static void main(String[] args) {
	Thread t1=new My1();
	t1.start();
//	t1.run();
	System.out.println(Thread.currentThread()+" end...");
}
}

class My1 extends Thread {
	@Override
	public void run() {
		new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread()+" end...");
			}
		}.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread()+" end...");
	}
}

第二种:

public class A2 {
	public static void main(String[] args) {
		Thread t1 = new Thread(new My2());
		t1.start();

//		省略了final 
		User tmp=new User();
		Thread t11=new Thread(new Runnable() {
			public void run() throws RuntimeException {
				tmp.id=100L;
			}
		});
		t11.start();
		try {
			t11.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(tmp);
		//语法错误
		new Thread(new Runnable() {
			public void run() throws IOException {

			}
		}).start();

		new Thread(() -> {
			System.out.println(Thread.currentThread());
			if(2>3)
				throw new RuntimeException("asdfasdf");
		}).start();
	}
}

class My2 implements Runnable {
	public void run() {
		System.out.println(Thread.currentThread());
	}
}

class User{
	Long id;
	String name;
}

第三种:

public class A3 {
	public static void main(String[] args) {
//	MyCallable2<Number> m2=new MyCallable2<Integer>()  错误
		MyCallable2<Number> m2 = new MyCallable2<>();
		m2.setT(123);
	}
}

class MyCallable1 implements Callable {
	@Override
	public Object call() throws Exception {
		if (1 == 2)
			return "asdfsd";
		else
			return 123;
	}
}

class MyCallable2<T> implements Callable<T> {
	private T t;

	@Override
	public T call() throws Exception {
		return t;
	}

	public T getT() {
		return t;
	}

	public void setT(T t) {
		this.t = t;
	}

}

class MyCallable3<T extends Number> implements Callable<T>{

	@Override
	public T call() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
} 
public class A31 {
	public static void main(String[] args) throws Exception {
//		FutureTask<Number> ft = new FutureTask<>(new My3());
//		new Thread(ft).start();
		Future<Number> ft = new FutureTask<>(new My3());
		if (ft != null && ft instanceof Runnable) {
			Runnable r = (Runnable) ft;
			new Thread(r).start();
		}
		Number obj = ft.get();
		System.out.println(obj.intValue());

		FutureTask<Number> ft2 = new FutureTask<>(() -> {
			return 123;
		});
		new Thread(ft2).start();
		int res = ft2.get().intValue();

		Future<Number> ft3 = new FutureTask<>(new Callable<Number>() {
			@Override
			public Number call() throws Exception {
				return 999;
			}
		});
		if (ft3 != null && ft3 instanceof Runnable) {
			Runnable r2 = (Runnable) ft3;
			new Thread(r2).start();
		}
	}
}

class My3 implements Callable<Number> {

	@Override
	public Number call() throws Exception {
		System.out.println(Thread.currentThread());
		return 123;
	}
}

第四种:

public class A4 {
	public static void main(String[] args) {
		ExecutorService es = Executors.newFixedThreadPool(5);
		for (int i = 0; i < 10; i++)
			es.execute(() -> {
				System.out.println(Thread.currentThread());
			});

//第二种写法
//		es.execute(new Runnable() {
//			public void run() {
//				
//			}
//		});
//		
//		es.submit(()->{
//			return "abc";
//		});
//		es.submit(()->{
//			System.out.println(Thread.currentThread());
//		});
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值