java中实现多线程的方法有两种:继承Thread类和实现runnable接口

1.继承Thread类,重写父类run()方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class thread1 extends Thread {
 
   public void run() {
       for (int i = 0; i < 10000; i++) {
           System.out.println("我是线程"+this.getId());
       }
   }
 
   public static void main(String[] args) {
       thread1 th1 = new thread1();
       thread1 th2 = new thread1();
       th1.run();
       th2.run();
   }
  }

run()方法只是普通的方法,是顺序执行的,即th1.run()执行完成后才执行th2.run(),这样写只用一个主线程。多线程就失去了意义,所以应该用start()方法来启动线程,start()方法会自动调用run()方法。上述代码改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class thread1 extends Thread {
      
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println("我是线程"+this.getId());
        }
    }
  
    public static void main(String[] args) {
        thread1 th1 = new thread1();
        thread1 th2 = new thread1();
        th1.start();
        th2.start();
    }
}

通过start()方法启动一个新的线程。这样不管th1.start()调用的run()方法是否执行完,都继续执行th2.start()如果下面有别的代码也同样不需要等待th2.start()执行完成,而继续执行。(输出的线程id是无规则交替输出的)

2.实现runnable接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class thread2 implements Runnable {
  
    public String ThreadName;
      
    public thread2(String tName){
        ThreadName = tName;
    }
      
      
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println(ThreadName);
        }
    }
      
    public static void main(String[] args) {
        thread2 th1 = new thread2("线程A");
        thread2 th2 = new thread2("线程B");
        th1.run();
        th2.run();
    }
}

和Thread的run方法一样Runnable的run只是普通方法,在main方法中th2.run()必须等待th1.run()执行完成后才能执行,程序只用一个线程。要多线程的目的,也要通过Thread的start()方法(注:runnable是没有start方法)。上述代码修改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class thread2 implements Runnable {
  
    public String ThreadName;
      
    public thread2(String tName){
        ThreadName = tName;
    }
      
      
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println(ThreadName);
        }
    }
      
    public static void main(String[] args) {
        thread2 th1 = new thread2("线程A");
        thread2 th2 = new thread2("Thread-B");
        Thread myth1 = new Thread(th1);
        Thread myth2 = new Thread(th2);
        myth1.start();
        myth2.start();
    }
}

3.使用ExecutorService、Callable、Future实现有返回结果的多线程(JDK5.0以后)
可返回值的任务必须实现Callable接口,类似的,无返回值的任务必须Runnable接口。执行Callable任务后,可以获取一个Future的对象,在该对象上调用get就可以获取到Callable任务返回的Object了,再结合线程池接口ExecutorService就可以实现传说中有返回结果的多线程了。下面提供了一个完整的有返回结果的多线程测试例子,在JDK1.5下验证过没问题可以直接使用。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.concurrent.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
   
/**
* 有返回值的线程
*/
@SuppressWarnings("unchecked")
public class Test {
public static void main(String[] args) throws ExecutionException,
  InterruptedException {
  System.out.println("----程序开始运行----");
  Date date1 = new Date();
   
  int taskSize = 5;
  // 创建一个线程池
  ExecutorService pool = Executors.newFixedThreadPool(taskSize);
  // 创建多个有返回值的任务
  List<Future> list = new ArrayList<Future>();
  for (int i = 0; i < taskSize; i++) {
  Callable c = new MyCallable(i + " ");
  // 执行任务并获取Future对象
  Future f = pool.submit(c);
  // System.out.println(">>>" + f.get().toString());
  list.add(f);
  }
  // 关闭线程池
  pool.shutdown();
   
  // 获取所有并发任务的运行结果
  for (Future f : list) {
  // 从Future对象上获取任务的返回值,并输出到控制台
  System.out.println(">>>" + f.get().toString());
  }
   
  Date date2 = new Date();
  System.out.println("----程序结束运行----,程序运行时间【"
   + (date2.getTime() - date1.getTime()) + "毫秒】");
}
}
   
class MyCallable implements Callable<Object> {
private String taskNum;
   
MyCallable(String taskNum) {
  this.taskNum = taskNum;
}
   
public Object call() throws Exception {
  System.out.println(">>>" + taskNum + "任务启动");
  Date dateTmp1 = new Date();
  Thread.sleep(1000);
  Date dateTmp2 = new Date();
  long time = dateTmp2.getTime() - dateTmp1.getTime();
  System.out.println(">>>" + taskNum + "任务终止");
  return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";
}
}

代码说明:
上述代码中Executors类,提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。
public static ExecutorService newFixedThreadPool(int nThreads)
创建固定数目线程的线程池。
public static ExecutorService newCachedThreadPool()
创建一个可缓存的线程池,调用execute 将重用以前构造的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线程并添加到池中。终止并从缓存中移除那些已有 60 秒钟未被使用的线程。
public static ExecutorService newSingleThreadExecutor()
创建一个单线程化的Executor。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
创建一个支持定时及周期性的任务执行的线程池,多数情况下可用来替代Timer类。
ExecutoreService提供了submit()方法,传递一个Callable,或Runnable,返回Future。如果Executor后台线程池还没有完成Callable的计算,这调用返回Future对象的get()方法,会阻塞直到计算完成。

总结:实现java多线程的2种方式,runable是接口,thread是类,runnable只提供一个run方法,建议使用runable实现

java多线程,不管如何,最终都需要通过thread.start()来使线程处于可运行状态。第三种方法是听群里的兄弟们介绍的,所以就百度补上了。

以上就是本文的全部内容,希望对大家的学习有所帮助。

详解三种java实现多线程的方式的更多相关文章

  1. 详解三种缓存过期策略LFU,FIFO,LRU(附带实现代码)

    在学操作系统的时候,就会接触到缓存调度算法,缓存页面调度算法:先分配一定的页面空间,使用页面的时候首先去查询空间是否有该页面的缓存,如果有的话直接拿出来,如果没有的话先查询,如果页面空间没有满的时候, ...

  2. Spring事务之详解--三种实现方式

    实现购买股票案例: 一.引入JAR文件: 二.开始搭建分层架构---创建账户(Account)和股票(Stock)实体类 Account: /* * 账户 */ public class Accoun ...

  3. Java 枚举(enum) 详解7种常见的用法

    Java 枚举(enum) 详解7种常见的用法 来源 https://blog.csdn.net/qq_27093465/article/details/52180865 JDK1.5引入了新的类型— ...

  4. Java网络编程和NIO详解开篇:Java网络编程基础

    Java网络编程和NIO详解开篇:Java网络编程基础 计算机网络编程基础 转自:https://mp.weixin.qq.com/s/XXMz5uAFSsPdg38bth2jAA 我们是幸运的,因为 ...

  5. Java网络编程和NIO详解5:Java 非阻塞 IO 和异步 IO

    Java网络编程和NIO详解5:Java 非阻塞 IO 和异步 IO Java 非阻塞 IO 和异步 IO 转自https://www.javadoop.com/post/nio-and-aio 本系 ...

  6. 转:Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种运行模式。

    原文来自于:http://www.ituring.com.cn/article/128439 Windows下的PHP开发环境搭建——PHP线程安全与非线程安全.Apache版本选择,及详解五种运行模 ...

  7. Java网络编程和NIO详解2:JAVA NIO一步步构建IO多路复用的请求模型

    Java网络编程与NIO详解2:JAVA NIO一步步构建IO多路复用的请求模型 知识点 nio 下 I/O 阻塞与非阻塞实现 SocketChannel 介绍 I/O 多路复用的原理 事件选择器与 ...

  8. 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)

    参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...

  9. Android 之窗口小部件详解(三)  部分转载

    原文地址:http://blog.csdn.net/iefreer/article/details/4626274. (一) 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget ...

随机推荐

  1. (转)Linux 命令(40): wc 命令

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 原文:http://blog.jobbole.com/109925/ http:/ ...

  2. CONCAT substr group_concat find_in_set

    (SELECT p.*,(SELECT CONCAT(m.name,m.id) FROM service_fastfix_category m WHERE m.id=SUBSTR(p.id,1,4)) ...

  3. hadoop操作权限问题:WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

    今天想从Eclipse向hdfs上传文件时遇到了一个权限问题,日志如下: ERROR hive.log: Got exception: org.apache.hadoop.security.Acces ...

  4. strstr strcpy 函数的实现

    一. strcpy 代码实现 #include <iostream> #include <assert.h> #include <iostream> //#incl ...

  5. iOS开发之数据存取

    http://www.cocoachina.com/ios/20141111/10190.html

  6. Arduino连接SHT10温湿度传感器--返回值不正常解决办法

    如题目,arduino中连接温湿度传感器,用的是一个github开源项目,地址:点击打开,其实这个就是一个封装好的库,下载后把解压的文件夹复制到Arduino目录下的librarys文件夹内,重启Ar ...

  7. java8 peek

    这样不会有任何的输出:Stream.of("one", "two", "three", "four").peek(e - ...

  8. hibernate课程 初探单表映射2-3 session简介

    hibernate流程: 1 配置对象Configurateion 读取 hibernate.cfg.xml 2 会话工厂SessionFactory 读取 user.hbm.xml(创建销毁相当耗费 ...

  9. js 数组操作常用方法

    push():在数组后面加入元素,并返回数组的长度: unshift():在数组前面就如元素,并返回数组的长度: pop():删除最后一个元素: var arr =[1,2,3,4,5] ; arr. ...

  10. tas5717/5719功放问题点总结

    问题一 AMP output channel invert problem and modification 原因:如果硬件设计时,耳机或者喇叭的输出通道反了,就要对其进行左右对换. 对策:如果通道反 ...