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 ...
随机推荐
- Linux中设置vim自动在运算符号两边加上空格
vim中设置自动在=+-之类的运算符号左右两边加上空格.原版的vim不带这个功能,写出的代码例如z=x+y,不美观,很多编译器(如VS)能够自动在符号两边加上空格,如z = x + y,看起来比较美观 ...
- easyui的增删改
陈旧的开发模式PM:“我要这个功能”后端:“这个先找前端做个模板”前端:“模板做完了”后端:“我来对接一下,这里样式不对”前端:“我改完了”后端:“功能交付”PM:“春节要加这个活动”后端:“这个先找 ...
- background--详解(背景图片根据屏幕的自适应)
background:有以下几种属性: background-color background-position background-size background-repeat backgroun ...
- SVG矢量动画
一.概述 相较于png.jpg等位图通过存储像素点来记录图像,svg (Scalable Vector Graphics)拥有一套自己的语法,通过描述的形式来记录图形.Android并不直接使用原始的 ...
- jq——html,text,val和对象访问
html代码 1.html():获取的是对象内的html代码,类似于innerHTML 2.html(代码):设置html的内容,替换 $("div").html("hh ...
- JS取出特定字符前后的字符串,针对一串字符里面的单个字符前后的字符串
//针对一串自负里面的单个字符前后的字符串<!doctype html> <html> <head> <meta charset="utf-8&qu ...
- There are multiple modules with names that only differ in casing.
client?4c0e:153 ./src/components/Paginate.vue There are multiple modules with names that only differ ...
- web前后端安全问题
1. 安全问题主要可以理解为以下两方面: 私密性:资源不被非法窃取和利用,只有在授权情况下才可以使用: 可靠性:资料不会丢失.损坏及篡改: 2. web安全的层面 代码层面:写代码时保证代码是安全的, ...
- BlankUtil(判断是否为空和去除多余空格)
package com.drn.core.util; import java.io.Serializable; import java.util.Map; import java.util.Prope ...
- 洛谷P5238 整数校验器
看到没有边读入边处理的,我来水一发 我们要看一下有那些情况是格式不合法的 单独的负号 -0(后面可以有其他数字) 0 +(后面一些数字) 我们用快速读入的方法 读取字符进行处理 还有可能超出范围的 考 ...