JavaThread、Runnable、Callable、线程池的使用

人生之路不会是一帆风顺的,我们会遇上顺境,也会遇上逆境,在所有成功路上折磨你的,背后都隐藏着激励你奋发向上的动机,人生没有如果,只有后果与结果,成熟,就是用微笑来面对一切小事。

导读:本篇文章讲解 JavaThread、Runnable、Callable、线程池的使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

目录

一、继承Thread类

二、实现Runnable接口

三、继承Callable接口

四、线程池


一、继承Thread类

public class TestThread1 extends Thread {
	
	@Override
	public void run() {
		for(int i=1; i<=100; i++) {
			System.out.println("线程1的" + i);
			try {
				sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

public class TestThread2 extends Thread {

	@Override
	public void run() {
		for(int i=1; i<=100; i++) {
			System.out.println("线程2的:" + i);
			try {
				sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
public class Test {

	public static void main(String[] args) {
		
		TestThread1 thread1 = new TestThread1();
		TestThread2 thread2 = new TestThread2();		
		
		thread1.start();
		thread2.start();
	}
}

        因为打一次要睡100ms,所以是一个线程一次

JavaThread、Runnable、Callable、线程池的使用

二、实现Runnable接口

public class TestRunnable1 implements Runnable {

	@Override
	public void run() {
		for(int i=0; i<100; i++) {
			System.out.println("线程1: " + i);
		}
	}

}
public class TestRunnable2 implements Runnable {

	@Override
	public void run() {
		for(int i=0 ;i<100; i++) {
			System.out.println("线程2: " + i);
		}
	}

}
public class Test {
	
	public static void main(String[] args) {
		
		TestRunnable1 run1 = new TestRunnable1();
		TestRunnable2 run2 = new TestRunnable2();
		
		new Thread(run1).start();
		new Thread(run2).start();
	}
}

JavaThread、Runnable、Callable、线程池的使用

三、继承Callable接口

public class TestCallable1 implements Callable<Integer> {

	@Override
	public Integer call() throws Exception {

		for(int i=0; i<100; i++) {
			System.out.println("线程1: " + i);
		}
		return null;
	}


}
public class TestCallable2 implements Callable<Integer> {

	@Override
	public Integer call() throws Exception {

		for(int i=0; i<100; i++) {
			System.out.println("线程2: " + i);
		}
		return 0;
	}

}
public class Test {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		
		FutureTask<Integer> task1 = new FutureTask<>(new TestCallable1());
		FutureTask<Integer> task2 = new FutureTask<>(new TestCallable2());
		new Thread(task1).start();
		new Thread(task2).start();
		
		System.out.println("[线程返回数据]:  " + task1.get());
		System.out.println("[线程返回数据]:  " + task2.get());

	}
}

四、线程池

public class TestThreadPool {

	public static void main(String[] args) {
		ExecutorService e = Executors.newCachedThreadPool();
		
		e.execute(new Test1());
		e.execute(new Test2());
	}
}

class Test1 implements Runnable{

	@Override
	public void run() {
		for(int i=0; i<100; i++) {
			System.out.println("线程1:" + i);
		}
	}
	
}

class Test2 implements Runnable{

	@Override
	public void run() {
		for(int i=0; i<100; i++) {
			System.out.println("线程2:" + i);
		}
	}
	
}

JavaThread、Runnable、Callable、线程池的使用

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/125031.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!