启动线程的3种方式
来源:互联网 发布:windows资源监视器 编辑:程序博客网 时间:2024/06/06 12:38
启动线程的3种方式
以下是3种方式:
1、继承Thread类
2、实现Runnable接口
3、实现Callable接口
对于2有以下优点:
1、实现接口还可以实现类
2、实现 接口可以让多个线程共享同一个target,即Runnable对象,可以更好地实现代码与数据的分离
以下是3种形式的代码:
1、继承Thread类
package ThreadText;
//第一种方式实现多线程,继承Thread类,重给run方法,不能有返回值,不能抛出异常
public class ThreadDemo1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
//在没有给线程起名字的时候默认是Thread-0
System.out.println(Thread.currentThread().getName() + "~~~" + i);
}
}
public static void main(String[] args) {
new ThreadDemo1().start();
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "~~~" + i);
}
}
}
//第一种方式实现多线程,继承Thread类,重给run方法,不能有返回值,不能抛出异常
public class ThreadDemo1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
//在没有给线程起名字的时候默认是Thread-0
System.out.println(Thread.currentThread().getName() + "~~~" + i);
}
}
public static void main(String[] args) {
new ThreadDemo1().start();
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "~~~" + i);
}
}
}
2、实现Runnable接口
package ThreadText;
//第二种方式实现Runnable,然后包装成Thread类,在调用start方法
public class ThreadDemo2 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
//在没有给线程起名字的时候默认是Thread-0
System.out.println(Thread.currentThread().getName() + "~~~" + i);
}
}
public static void main(String[] args) {
//将Runnable的实现类包装成Thread对象,然后才能调用‘Thread’的‘start’方法!!
//这是api: Thread(Runnable target)
// Allocates a new Thread object.
new Thread(new ThreadDemo2()).start();
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "~~~" + i);
}
}
}
//第二种方式实现Runnable,然后包装成Thread类,在调用start方法
public class ThreadDemo2 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
//在没有给线程起名字的时候默认是Thread-0
System.out.println(Thread.currentThread().getName() + "~~~" + i);
}
}
public static void main(String[] args) {
//将Runnable的实现类包装成Thread对象,然后才能调用‘Thread’的‘start’方法!!
//这是api: Thread(Runnable target)
// Allocates a new Thread object.
new Thread(new ThreadDemo2()).start();
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "~~~" + i);
}
}
}
3、实现Callable接口
package ThreadText;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
//第三种方式,先得到Callable的实现类,包装 成 Runnable实现类(FutureTask),然后包装成 Thread的实现类,调用start函数
public class ThreadDemo3 implements Callable<Integer>{
@Override
public Integer call() throws Exception {
int count = 0;
for (int i = 0; i < 100; i++) {
//在没有给线程起名字的时候默认是Thread-0
System.out.print(Thread.currentThread().getName() + "~~~" + i + " ");
count += i;
}
System.out.println();
return count;
}
public static void main(String[] args) throws Exception{
ThreadDemo3 td = new ThreadDemo3();//生成一个Callable的实现类,实现了里面的call函数
FutureTask ft = new FutureTask(td);//将Callable实现类包装成Runnable的实现类,这里是FutureTask
Thread td2 = new Thread(ft);//这里将Runnable实现类包装成Thread类型的
td2.start();//总之是要调用Thread的start()函数
//这里用Runnable的实现类得到Call()函数的返回值
System.out.println(ft.get());
for (int i = 0; i < 100; i++) {
System.out.print(Thread.currentThread().getName() + "~~~" + i + " " );
}
}
}
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
//第三种方式,先得到Callable的实现类,包装 成 Runnable实现类(FutureTask),然后包装成 Thread的实现类,调用start函数
public class ThreadDemo3 implements Callable<Integer>{
@Override
public Integer call() throws Exception {
int count = 0;
for (int i = 0; i < 100; i++) {
//在没有给线程起名字的时候默认是Thread-0
System.out.print(Thread.currentThread().getName() + "~~~" + i + " ");
count += i;
}
System.out.println();
return count;
}
public static void main(String[] args) throws Exception{
ThreadDemo3 td = new ThreadDemo3();//生成一个Callable的实现类,实现了里面的call函数
FutureTask ft = new FutureTask(td);//将Callable实现类包装成Runnable的实现类,这里是FutureTask
Thread td2 = new Thread(ft);//这里将Runnable实现类包装成Thread类型的
td2.start();//总之是要调用Thread的start()函数
//这里用Runnable的实现类得到Call()函数的返回值
System.out.println(ft.get());
for (int i = 0; i < 100; i++) {
System.out.print(Thread.currentThread().getName() + "~~~" + i + " " );
}
}
}
需要注意的内容都在代码中说明了,自己查看,欢迎各位指教^ _ ^!!
0 0
- 启动线程的3种方式
- 线程的启动方式
- 启动线程的两种方式
- java 启动线程的两种方式
- 启动线程的五种方式
- Android启动线程的四种方式
- 启动线程几种方式
- 常用 java启动线程 的2种方式
- 创建和启动线程的两种方式
- java中启动线程的两种方式
- Java多线程(二)、启动一个线程的3种方式
- 线程的实现及其启动方式
- JAVA中两种启动线程方式的区别
- C++11启动线程的多种方式
- java 启动线程三种方式
- java 启动线程三种方式
- java 启动线程三种方式
- Java 启动线程三种方式
- [李景山php] linux
- Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.4.1:clean (default-clean) 解决办法
- Jtag 10针和20针接口转换
- [李景山php] -linux -用户组
- 打开方式
- 启动线程的3种方式
- 【机器学习PAI实践六】金融贷款发放预测
- IOS开发之MVC模式(根据网易斯坦福大学公开课整理)
- android之鲜为人知的Intent功能
- Windows 和 ubuntu 共享目录
- 接口性能测试方案
- [李景山php] linux 网络
- cookie跨域存取
- [李景山php] linux 服务及进程