好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

多线程Thread,Runnable,Callable实现方式

一、创建线程的常用三种方式

1、继承Thread类

创建MyThread 类并继承Thread

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.zrrd.XianCheng;

/**

  *继承Thread类,达到线程的能力

  */

public class MyThread extends Thread{

     private String name;

     public MyThread(String name){

          this .name=name;

     }

     @Override

     public void run() {

           for ( int i= 0 ;i<= 100 ;i++){

               System.out.println(name+ "下载了" +i+ "%" );

           }

     }

}

创建测试类ThreadText 启动线程,执行代码块

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.zrrd.XianCheng;

/**

  * 测试线程代码

  */

public class ThreadText {

     public static void main(String[] args) {

         //创建一个线程对象

          MyThread my= new MyThread( "A线程" );

          //启动线程start()

          my.start();

         //创建第二个线程对象

         MyThread my1= new MyThread( "B线程" );

         //启动线程start()

         my1.start();

     }

}

执行结果图片,截取部分结果集

2、实现Runnable接口(重点)

以多个线程并发,解决方法为例

**创建BingFa 类并实现Runnable **

?

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

package com.zrrd.XianCheng;

/**

  * 多个线程并发,解决方法

  */

public class BingFa implements Runnable {

     int piaoshu= 50 ;

     Object obj= new Object();

     @Override

     public void run() {

         /* 同步代码块用synchronized修饰*/

         while ( true ){

           synchronized (obj){ //发生阻塞事件

                    if (piaoshu> 0 ){

                        System.out.println(Thread.currentThread().getName()+ "剩余" +(piaoshu--)+ "张" );

                    } else {

                        break ;

                    }

              }

               try {

                   Thread.sleep( 200 );

               } catch (InterruptedException e) {

                   e.printStackTrace();

               }

           }

         System.out.println(Thread.currentThread().getName()+ "结束售票" );

     }

}

创建测试类BingFaText启动线程,执行代码块

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package com.zrrd.XianCheng;

import java.lang.Thread;

public class BingFaText {

     public static void main(String[] args) {

          BingFa bf= new BingFa();

          Thread th1= new Thread(bf, "售票窗口1" );

          Thread th2= new Thread(bf, "售票窗口2" );

          Thread th3= new Thread(bf, "售票窗口3" );

          Thread th4= new Thread(bf, "售票窗口4" );

          th1.start();

          th2.start();

          th3.start();

          th4.start();

     }

}

执行结果图片,截取部分结果集

3、 实现Callable接口(JDK1.5版本之后引入的)

创建MyCallable类并实现Callable<T>

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.zrrd.XianCheng;

import java.util.concurrent.Callable;

public class MyCallable implements Callable<String> { //多线程主体类

     private int ticket= 5 ;                     //线程共享资源

     @Override

     public String call() throws Exception {

             for ( int i= 0 ;i< 100 ;i++){

                 if ( this .ticket> 0 ){

                     System.out.println( "卖票,ticket=" + this .ticket--);

                 }

             }

         return "票已卖光!" ;        //返回结果

     }

}

创建测试类MyCallableDemo启动线程,执行代码块

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.zrrd.XianCheng;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.FutureTask;

public class MyCallableDemo {

     public static void main(String[] args) throws ExecutionException, InterruptedException {

         MyCallable callable1 = new MyCallable(); //创建多线程实例1

         MyCallable callable2 = new MyCallable(); //创建多线程实例2

         FutureTask task1 = new FutureTask<String>(callable1);

         FutureTask task2 = new FutureTask<String>(callable2);

         //FutureTask 是Runnable接口的子类,所以可以使用Thread类的构造方法来接收task对象

         new Thread(task1).start();

         new Thread(task2).start();

         System.out.println( "A______" +task1.get());

         System.out.println( "B______" +task2.get());

     }

}

执行结果图片

总结

以上就是实现多线程的方式,在实际开发项目中不建议使用第一种继承Thread方式实现线程,这样对代码的侵入性太高,而且类与类之间单继承,达不到代码重用规则,建议使用第二种、或第三种。

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://blog.csdn.net/zhx__/article/details/119851683

查看更多关于多线程Thread,Runnable,Callable实现方式的详细内容...

  阅读:13次