java 多线程学习(Java中Thread 和 Runnable的区别)

来源:互联网 发布:武汉有单片机培训机构 编辑:程序博客网 时间:2024/04/28 19:49

今天刚学习java多线程

在网上查了几篇博文学习,个人觉得写的不错,但是有的地方有个疑问:Java中Thread 和 Runnable的区别。

然后,在网上查找相关资料,发现几乎都是一样的。

下面贴出相关的代码:

如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享。

/**
 * @author Rollen-Holt 继承Thread类,不能资源共享
 * */
class hello extends Thread {
    public void run() {
        for (int i = 0; i < 7; i++) {
            if (count > 0) {
                System.out.println("count= " + count--);
            }
        }
    }
 
    public static void main(String[] args) {
        hello h1 = new hello();
        hello h2 = new hello();
        hello h3 = new hello();
        h1.start();
        h2.start();
        h3.start();
    }
 
    private int count = 5;
}

【运行结果】:

count= 5

count= 4

count= 3

count= 2

count= 1

count= 5

count= 4

count= 3

count= 2

count= 1

count= 5

count= 4

count= 3

count= 2

count= 1

大家可以想象,如果这个是一个买票系统的话,如果count表示的是车票的数量的话,说明并没有实现资源的共享。

我们换为Runnable接口

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MyThread implementsRunnable{
 
    private int ticket = 5//5张票
 
    public void run() {
        for (int i=0; i<=20; i++) {
            if (this.ticket >0) {
                System.out.println(Thread.currentThread().getName()+"正在卖票"+this.ticket--);
            }
        }
    }
}
public class lzwCode {
     
    public static void main(String [] args) {
        MyThread my = new MyThread();
        new Thread(my, "1号窗口").start();
        new Thread(my, "2号窗口").start();
        new Thread(my, "3号窗口").start();
    }
}

 

  

 

 

【运行结果】:

count= 5

count= 4

count= 3

count= 2

count= 1

这是博主的代码及运行结果,当时觉得没什么问题,当我自己运行第二段代码后将MyThread实现的runnable接口改为Thread方法就发现问题了,下面是修改后的代码及执行结果:

class MyThread extends Thread{
 
    private int ticket = 5;  //5张票
 
    public void run() {
        for (int i=0; i<=20; i++) {
            if (this.ticket > 0) {
                System.out.println(Thread.currentThread().getName()+ "正在卖票"+this.ticket--);
            }
        }
    }
}

public class Test {

    public static void main(String [] args) {
        MyThread my = new MyThread();
        new Thread(my, "1号窗口").start();
        new Thread(my, "2号窗口").start();
        new Thread(my, "3号窗口").start();
    }
}
执行结果:

然后我又回去仔细的看了下代码,发现博主的第一个程序和第二个程序的可比性不高,所以其解释是正确的,但是代码并不是很好。

个人整理校正如下:

class MyThread extends Thread{
 
    private int ticket = 5;  //5张票
 
    public void run() {
        for (int i=0; i<=20; i++) {
            if (this.ticket > 0) {
                System.out.println(Thread.currentThread().getName()+ "正在卖票"+this.ticket--);
            }
        }
    }
}

class R implements Runnable{
   private int ticket = 5;  //5张票
    @Override
    public void run() {
         for (int i=0; i<=20; i++) {
                if (this.ticket > 0) {
                    System.out.println(Thread.currentThread().getName()+ "正在卖票"+this.ticket--);
                }
            }
    }
    
}


public class Test {

    public static void main(String [] args) {
        R  r = new R();
        for(int i=0 ;i<10 ;i++){
            Thread tr = new Thread(r);
            tr.start();
        }
        
        for(int i=0 ;i<5 ;i++){
            Thread tr = new MyThread();
            tr.start();
        }
    }
}

执行结果如下:

第一个:

第二个:

Thread-0正在卖票5
Thread-0正在卖票4
Thread-0正在卖票3
Thread-0正在卖票2
Thread-0正在卖票1
Thread-1正在卖票5
Thread-4正在卖票5
Thread-4正在卖票4
Thread-4正在卖票3
Thread-4正在卖票2
Thread-4正在卖票1
Thread-1正在卖票4
Thread-1正在卖票3
Thread-1正在卖票2
Thread-1正在卖票1
Thread-3正在卖票5
Thread-3正在卖票4
Thread-3正在卖票3
Thread-3正在卖票2
Thread-3正在卖票1
Thread-2正在卖票5
Thread-2正在卖票4
Thread-2正在卖票3
Thread-2正在卖票2
Thread-2正在卖票1

这个是在一个网友的文档上读到的,main方法中的两个方法,我们可以显然的将结果比较出来,他们new的方式一样,只是第一个将所有的线程建立在同一个对象上,新建的所有线程只对同一个对象进行操作。而第二个是new了很多的Thread,这样就建立了很多的对象,他们之间的操作是互补干扰的,只是之间互相争夺CPU的资源。(Runnable共享资源的实质)

推荐学习博文:http://developer.51cto.com/art/201203/321042.htm

0 0
原创粉丝点击