服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|

服务器之家 - 编程语言 - JAVA教程 - 浅谈Java的两种多线程实现方式

浅谈Java的两种多线程实现方式

2020-12-21 11:09翡翠森林Z JAVA教程

本篇文章主要介绍了浅谈Java的两种多线程实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文介绍了浅谈Java的两种多线程实现方式,分享给大家。具有如下:

一、创建多线程的两种方式

Java中,有两种方式可以创建多线程:

1 通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中

2 通过实现Runnable接口,实例化Thread类
在实际应用中,我们经常用到多线程,如车站的售票系统,车站的各个售票口相当于各个线程。当我们做这个系统的时候可能会想到两种方式来实现,继承Thread类或实现Runnable接口,现在看一下这两种方式实现的两种结果。

程序1:

?
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
package z;
 
class MyThread extendsThread{ 
 
  privateintticket = 10;
 
  privateString name;
 
  publicMyThread(String name){
 
    this.name =name;
 
  }
 
   
 
  publicvoidrun(){
 
    for(inti = 0; i < 500; i++){
 
      if(this.ticket > 0){
 
        System.*out*.println(this.name+"卖票---->"+(this.ticket--));
 
      }
 
    }
 
  }
 
}
 
 
 
public classThreadDemo {
 
  publicstaticvoidmain(String[] args) {
 
    MyThread mt1= newMyThread("一号窗口");
 
    MyThread mt2= newMyThread("二号窗口");
 
    MyThread mt3= newMyThread("三号窗口");
 
    mt1.start();
 
    mt2.start();
 
    mt3.start();
 
  }
 
}

运行结果:

一号窗口卖票---->10

一号窗口卖票---->9

一号窗口卖票---->8

一号窗口卖票---->7

一号窗口卖票---->6

一号窗口卖票---->5

一号窗口卖票---->4

一号窗口卖票---->3

一号窗口卖票---->2

一号窗口卖票---->1

三号窗口卖票---->10

三号窗口卖票---->9

三号窗口卖票---->8

三号窗口卖票---->7

二号窗口卖票---->10

二号窗口卖票---->9

二号窗口卖票---->8

三号窗口卖票---->6

三号窗口卖票---->5

三号窗口卖票---->4

三号窗口卖票---->3

二号窗口卖票---->7

二号窗口卖票---->6

二号窗口卖票---->5

二号窗口卖票---->4

二号窗口卖票---->3

二号窗口卖票---->2

二号窗口卖票---->1

三号窗口卖票---->2

三号窗口卖票---->1

程序2:

?
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
package z;
 
class MyThread1 implementsRunnable{
 
   privateintticket =10;
 
   publicvoidrun(){
 
      for(inti = 0; i<500; i++){
 
         if(this.ticket>0){
 
               System.*out*.println(Thread.*currentThread*().getName()
+ "卖票---->"+ (this.ticket--));
 
         }
 
      }
 
   }
 
}
 
public classRunnableDemo {
 
   publicstaticvoidmain(String[] args) {
 
      // 设计三个线程
 
      MyThread1 mt = newMyThread1();
 
      Thread t1 = newThread(mt, "一号窗口");
 
      Thread t2 = newThread(mt, "二号窗口");
 
      Thread t3 = newThread(mt, "三号窗口");
 
      t1.start();
 
      t2.start();
 
      t3.start();
 
   }
 
}

运行结果:

三号窗口卖票---->10

三号窗口卖票---->7

三号窗口卖票---->6

三号窗口卖票---->5

三号窗口卖票---->4

三号窗口卖票---->3

一号窗口卖票---->8

二号窗口卖票---->9

一号窗口卖票---->1

三号窗口卖票---->2

为什么两个程序的结果不同呢?

第1个程序,相当于拿出三件事即三个卖票10张的任务分别分给三个窗口,他们各做各的事各卖各的票各完成各的任务,因为MyThread继承Thread类,所以在new MyThread的时候在创建三个对象的同时创建了三个线程。

第2个程序,相当于是拿出一个卖票10张得任务给三个人去共同完成,new MyThread相当于创建一个任务,然后实例化三个Thread,创建三个线程即安排三个窗口去执行。

用图表示如下:

浅谈Java的两种多线程实现方式

通过上面的分析,我们发现这两种多线程有两大区别:

(1)Thread方式是继承;Runnable方式是实现接口。

(2)Thread方式是多个线程分别完成自己的任务,即数据独立;Runnable方式是多个线程共同完成一个任务,即数据共享。

大多数情况下,如果只想重写run() 方法,而不重写其他 Thread 方法,那么应使用 Runnable 接口。这很重要,因为除非程序员打算修改或增强类的基本行为,否则不应为该类(Thread)创建子类。

二、隐藏的问题

在第二种方法中,由于3个Thread对象共同执行一个Runnable对象中的代码,因此可能会造成线程的不安全,比如可能ticket会输出-1(如果我们System.out....语句前加上线程休眠操作,该情况将很有可能出现)。

这种情况的出现是由于,一个线程在判断ticket为1>0后,还没有来得及减1,另一个线程已经将ticket减1,变为了0,那么接下来之前的线程再将ticket减1,便得到了-1。

这就需要加入同步操作(即互斥锁),确保同一时刻只有一个线程在执行每次for循环中的操作。

而在第一种方法中,并不需要加入同步操作,因为每个线程执行自己Thread对象中的代码,不存在多个线程共同执行同一个方法的情况。

程序1:

?
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
60
package z;
 
class MyThread1 implementsRunnable{
 
   privateintticket = 10;
 
   publicvoidrun(){
 
      for(inti = 0; i<500; i++){
 
         if(this.ticket>0){
 
            try{
 
               Thread.*sleep*(100);
 
               System.*out*.println(Thread.*currentThread*().getName()
+ "卖票---->"+ (this.ticket--));
 
            }catch(Exception e) {
 
               e.printStackTrace();
 
            }
 
         }
 
      }
 
   }
 
}
 
 
 
public classRunnableDemo {
 
 
 
   publicstaticvoidmain(String[] args) {
 
      // 设计三个线程
 
      MyThread1 mt = newMyThread1();
 
      Thread t1 = newThread(mt, "一号窗口");
 
      Thread t2 = newThread(mt, "二号窗口");
 
      Thread t3 = newThread(mt, "三号窗口");
 
      t1.start();
 
      t2.start();
 
      t3.start();
 
   }
 
}

运行结果:

一号窗口卖票---->10

二号窗口卖票---->10

三号窗口卖票---->9

一号窗口卖票---->8

三号窗口卖票---->7

二号窗口卖票---->8

一号窗口卖票---->6

三号窗口卖票---->4

二号窗口卖票---->5

三号窗口卖票---->3

二号窗口卖票---->2

一号窗口卖票---->3

二号窗口卖票---->1

三号窗口卖票---->-1

一号窗口卖票---->0

程序2:

?
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
package z;
 
class MyThread1 implementsRunnable{
 
   privateintticket = 1000;
 
   publicvoidrun(){
 
      for(inti = 0; i<5000; i++){
 
         synchronized(this) {
 
            if(this.ticket>0){
 
               System.*out*.println(Thread.*currentThread*().getName()+"卖票---->"+(this.ticket--));
 
            }
 
         }
 
      }
 
   }
 
}
 
 
 
public classRunnableDemo {
 
 
 
   publicstaticvoidmain(String[] args) {
 
      // 设计三个线程
 
      MyThread1 mt = newMyThread1();
 
      Thread t1 = newThread(mt, "一号窗口");
 
      Thread t2 = newThread(mt, "二号窗口");
 
      Thread t3 = newThread(mt, "三号窗口");
 
      t1.start();
 
      t2.start();
 
      t3.start();
 
   }
 
}

运行结果:

一号窗口卖票---->10

一号窗口卖票---->9

一号窗口卖票---->8

一号窗口卖票---->7

一号窗口卖票---->6

一号窗口卖票---->5

一号窗口卖票---->4

一号窗口卖票---->3

一号窗口卖票---->2

一号窗口卖票---->1

注意,这里的10张票都是一号窗口卖出的。这是因为用了synchronized并且票数太少了,在t1对this对象锁定的时间内,10张票就已经被卖完了。轮到t2或t3锁定this对象时,已经无票可卖了。如果票数多一点,比如有几万张,就可以看到三个窗口都参与了卖票。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.jianshu.com/p/098ee2a89e6f

延伸 · 阅读

精彩推荐