黑马程序员_java多线程总结

java中的多线程
在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口。
那么:为什么我们不能直接调用run()方法呢?
我的理解是:线程的运行需要本地操作系统的支持。
如果你查看start的源代码的时候,会发现:
注意我用红色加粗的那一条语句,说明此处调用的是start0()。并且这个这个方法用了native关键字,次关键字表示调用本地操作系统的函数。因为多线程的实现需要本地操作系统的支持。
class hello extends Thread {
  
    public hello() {
  
    }
  
    public hello(String name) {
        this.name = name;
    }
  
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(name + "运行     " + i);
        }
    }
  
    public static void main(String[] args) {
        hello h1=new hello("A");
        hello h2=new hello("B");
        h1.start();
        h2.start();
    }
  
    private String name;
}
 
 
class hello implements Runnable {
  
    public hello() {
  
    }
  
    public hello(String name) {
        this.name = name;
    }
  
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(name + "运行     " + i);
        }
    }
  
    public static void main(String[] args) {
        hello h1=new hello("线程A");
        Thread demo= new Thread(h1);
        hello h2=new hello("线程B");
        Thread demo1=new Thread(h2);
        demo.start();
        demo1.start();
    }
  
    private String name;
}
 
 
Thread和Runnable的区别:
如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享。
class MyThread implements Runnable{
  
    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();
    }
}
 
 
总结一下吧:
 
实现Runnable接口比继承Thread类所具有的优势:
 
1):适合多个相同的程序代码的线程去处理同一个资源
 
2):可以避免java中的单继承的限制
 
3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。
 
 
【使用线程同步解决问题】
 
采用同步的话,可以使用同步代码块和同步方法两种来完成。
 
class hello implements Runnable {
    public void run() {
        for(int i=0;i<10;++i){
            synchronized (this) {
                if(count>0){
                    try{
                        Thread.sleep(1000);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(count--);
                }
            }
        }
    }
  
    public static void main(String[] args) {
        hello he=new hello();
        Thread h1=new Thread(he);
        Thread h2=new Thread(he);
        Thread h3=new Thread(he);
        h1.start();
        h2.start();
        h3.start();
    }
    private int count=5;
}
 
也可以采用同步方法。
 
语法格式为synchronized 方法返回类型方法名(参数列表){
 
    // 其他代码
 
}
 
class hello implements Runnable {
    public void run() {
        for (int i = 0; i < 10; ++i) {
            sale();
        }
    }
  
    public synchronized void sale() {
        if (count > 0) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(count--);
        }
    }
  
    public static void main(String[] args) {
        hello he = new hello();
        Thread h1 = new Thread(he);
        Thread h2 = new Thread(he);
        Thread h3 = new Thread(he);
        h1.start();
        h2.start();
        h3.start();
    }
  
    private int count = 5;
}
posted @ 2015-04-14 17:46  计无施  阅读(150)  评论(0编辑  收藏  举报