Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55668
  • 博文数量: 51
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2018-08-26 01:30
文章分类

全部博文(51)

文章存档

2020年(2)

2018年(49)

我的朋友

分类: Java

2018-08-29 05:21:34

Example2:



  1. public class RunnableThread implements Runnable {
  2.     public int mul(int x, int y) {
  3.         int result = 0;
  4.         result = x * y; return result;
  5.     }
  6.     
  7.     public void run() {
  8.         // TODO Auto-generated method stub
  9.         for (int i = 0; i < 5; i++) {
  10.             for (int j = 0; j < 5; j++) {
  11.                 System.out.print("x: " + i + " y: " + j + " result: ");
  12.                 System.out.println(mul(i, j));
  13.             }
  14.         }
  15.     }
  16. }



我们可以使用以下方式:


1. v1.5版本之前


        Thread rt = new Thread(new RunnableThread());
        rt.start();



2.v1.5版本之后


        ExecutorService exec = Executors.newCachedThreadPool();
        exec.execute(new RunnableThread());



下次,我们讲线程的同步。

阅读(385) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~