多线程

多个线程组成一个进程

多线程的两个写法:

1.

 

public class MyThread extends Thread {
    //继承Thread类
    //单继承  不能继承多个
    public first f;
    @Override
    public void run(){
        for (int i = 1; i <=20; i++) {
            f.run1();
        }
    }
}

2.

public class MyThread2 implements Runnable {
    //接口
    //方便拓展  可以实现多个接口
    public first f;
    @Override
    public void run() {
        for (int i = 1; i <=20; i++) {
            f.run2();
        }

    }

}

调用:

public class Text {

    public static void main(String[] args) {
        first fs=new first();
        
        MyThread myThread=new MyThread();
        myThread.f=fs;
        myThread.start();
        
        MyThread2 myThread2=new MyThread2();
        myThread2.f=fs;
        Thread thread=new Thread(myThread2);
        thread.start();

    }

}

 

 

线程的同步和异步

同步:

多个线程依次执行

异步:

多个线程同时执行

posted on 2016-08-14 17:37  -EASY-  阅读(221)  评论(0编辑  收藏  举报

导航