java 实现多线程 3种方式
java实现多线程可以有以下三种方式:
(1)继承Thread 类,重写其run()方法;
(2)实现Runnable接口,实现其run() 方法;
(3) 实现Callable 接口,重写call() 方法;
下面以实际的例子做一下展示
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; class MyThread extends Thread{
//采用集成的方式实现线程
public void run(){
for(int i=0; i<=5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("MyThread 集成方式实现 the i ="+i); }
} } class MyImpThread implements Runnable{ @Override
public void run() {
for(int i=0; i<=5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("MyImpThread 实现接口的方式实现 the i ="+i); } } } class CallableTest implements Callable<Object>{ @Override
public Object call() throws Exception {
for(int i=0; i<=5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Callable类型 实现接口的方式实现 the i ="+i); }
return "Callable 接口好";
} } public class TestThread { public static void main(String[] args) {
//1.集成方式
MyThread my = new MyThread();
my.start(); //2.实现接口方式
Thread thread = new Thread(new MyImpThread());
thread.start(); //3.实现Callable 接口
ExecutorService threadPool = Executors.newSingleThreadExecutor();
Future<Object> future = threadPool.submit(new CallableTest()); try {
Thread.sleep(2000); //让主线程先休眠2s;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} for(int i=0; i<=5;i++){
System.out.println("当前主线程 the i ="+i); } } }
运行结果:
MyImpThread 实现接口的方式实现 the i =0
MyThread 集成方式实现 the i =0
Callable类型 实现接口的方式实现 the i =0
MyImpThread 实现接口的方式实现 the i =1
MyThread 集成方式实现 the i =1
Callable类型 实现接口的方式实现 the i =1
当前主线程 the i =0
当前主线程 the i =1
当前主线程 the i =2
当前主线程 the i =3
当前主线程 the i =4
当前主线程 the i =5
MyImpThread 实现接口的方式实现 the i =2
MyThread 集成方式实现 the i =2
Callable类型 实现接口的方式实现 the i =2
MyImpThread 实现接口的方式实现 the i =3
MyThread 集成方式实现 the i =3
Callable类型 实现接口的方式实现 the i =3
MyImpThread 实现接口的方式实现 the i =4
MyThread 集成方式实现 the i =4
Callable类型 实现接口的方式实现 the i =4
MyImpThread 实现接口的方式实现 the i =5
MyThread 集成方式实现 the i =5
Callable类型 实现接口的方式实现 the i =5
可以看出,三种方式实现的java线程都可以很好运行,加上主线程,一共四个线程在同时运行,各个线程之间来回切换。
java 实现多线程 3种方式的更多相关文章
- Java序列化的几种方式以及序列化的作用
Java序列化的几种方式以及序列化的作用 本文着重讲解一下Java序列化的相关内容. 如果对Java序列化感兴趣的同学可以研究一下. 一.Java序列化的作用 有的时候我们想要把一个Java对象 ...
- Java创建对象的4种方式?
[Java创建对象的4种方式?] 1)通过new语句实例化一个对象 2)通过反射机制创建对象 3)通过clone()方法创建一个对象 (复制) 4)通过反序列化方式创建对象
- Java创建对象的几种方式
解析:Java创建对象的几种方式(重要):(1) 用new语句创建对象,这是最常见的创建对象的方法.(2) 运用反射手段,调用java.lang.Class或者java.lang.reflect.Co ...
- &和&&的共同点和区别、Java字符含义和Java创建对象的几种方式
一.&和&&的共同点和区别 1.&和&&的联系(共同点): &和&&都可以用作逻辑与运算符,但是要看使用时的具体条件来决定. 操 ...
- Java创建对象的几种方式。
Java创建对象的几种方式(重要): (1) 用new语句创建对象,这是最常见的创建对象的方法. (2) 运用反射手段,调用java.lang.Class或者java.lang.reflect.Con ...
- 使用Maven运行Java main的3种方式
使用Maven运行Java main的3种方式 原文 http://blog.csdn.net/qbg19881206/article/details/19850857 主题 Maven maven ...
- 面试阿里,字节跳动,华为必须知道的Java创建对象的5种方式
Java创建对象的5种方式 1.直接new,调用了构造器2.通过clone(),没有调用构造器3.通过反射,调用了构造器4.通过反序列化,没有调用构造器5.通过Unsafe类的allocateInst ...
- java多线程3种方式
Java多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中前两种方式线程执行完后都没 ...
- java多线程三种方式
java多线程都有几种方式 有三种: (1)继承Thread类,重写run函数 创建: class xx extends Thread{ public void run(){ Thread.sleep ...
随机推荐
- SQL Server 2014 中,新建登录用户,分配权限,并指定该用户的数据
一.运行环境 系统:Windows 10数据库:SQL Server 2014数据库名: APP 新建的用户名: app 二.操作步骤 1.打开 MS SQL Server Managemen ...
- Windows环境下配置thrift编译器(VS2015)
参考官方文档:http://svn.apache.org/repos/asf/thrift/attic/trunk/compiler/cpp/README_Windows.txt 编译器( ...
- oracle调优使用到相关sql
select * from v$session where username is not null;select username,count(username) from v$session wh ...
- ZBrush常用快捷键
ZBrush是一款数字雕刻和绘画软件,它以强大的功能和直观的工作流程彻底改变了整个三维雕刻行业.强大的功能离不开便捷的操作,为此ZBrush®提供了一系列常用操作快捷键,熟练掌握这些快捷键,可帮助您节 ...
- [luogu2501 HAOI2006] 数字序列 (递推LIS)
题目描述 现在我们有一个长度为n的整数序列A.但是它太不好看了,于是我们希望把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希望改变的幅度太大. 输入输出格式 输入格式: 第一行包含一个数 ...
- PHP动态函数处理
public class Student{ public function speek($name){ echo 'my name is '.$name; } } $method='speek'; $ ...
- QT中文字的绘制
为什么要做这次文字的介绍,因为在一般的教材中,还真没有文字的描述: 1.绘制最简单的文字. 我们更改重绘函数如下: void Dialog::paintEvent(QPaintEvent *){QPa ...
- MyEclipse 设置JSP,HTML的默认打开方式,避免出现打开后上面出现浏览器
1. 2. 3. jsp的设置一样,这样myeclipse打开jsp就不会出现上面的浏览器了
- Java线程:CountDownLatch 与Thread 的 join()
需求: 主程序中需要等待所有子线程完成后 再继续任务 两种实现方式: 一种使用join() 方法:当在当前线程中调用某个线程 thread 的 join() 方法时,当前线程就会阻塞,直到thread ...
- pip安装-mac电脑
If you meant "pip" specifically: Homebrew provides pip via: `brew install python`. However ...