黑马程序员——线程实现的两种方法

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

线程实现的两种方法:

1、继承Thread。

2、实现Runnable接口,同时覆run()方法。

  继承Thread实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MyThreadextends Thread {
    private String name;
    public MyThread(String name)
    {
        this.name = name;
    }
    public void run()
    {
        for(int i=0;i<10;i++)
            System.out.println(this.name+"---线程启动"+i);
    }
}
public class MyThreadDemo {
    public static void main(String args[])
    {
        MyThread mt = new MyThread("自定义线程");
        mt.start();
    }
}

  实现Runnable接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class MyThread1implements Runnable
{
    public void run() {
        for(int i=0;i<10;i++) {
            System.out.println("--线程启动--"+i);
        }
                                
    }
}
 
 
public class MyThreadDemo1 {
    public static void main(String args[]) {
        MyThread1
        mt = new MyThread1();
        Thread t = new Thread(mt);
        t.start();
         
    }
}

 

  ★在这里为什么要使用start()方法启动线程呢?

通过查阅可以看到Runnable接口如下:

1
2
3
4
5
interface Runnable
{
    public void run();
}
 .......... Thread implements Runnable {   //其他方法   publicc void run(){}   public native void start(); }  

   Runable子类中并没有start()方法,而只有Thread类中才有,如果直接调用run()方法,

程序将会运行完run()方法体中的语句再运行后面的语句,根本达不到多线程的目的;

调用没有方法体的start()方法,native关键字声明的方法没有方法体,使用此关键字表示可调

用操作系统的底层函数(那么这样的技术又称为JNI技术),而且此方法在执行时将调用run()

方法(由系统默认调用)。

  ★在使用Runnable子类中并没有start()方法,而只有Thread类中才有,在Thread类中存在

以下构造方法:

  public Thread(Runnable target)

此构造方法接收Runnabled 子类实例。

  ★两种实例的区别(实现Runnable的好处)

1、避免单继承。

2、实现资源共享。(以卖票为例)

 

  Thread在卖张票三个线程同时卖票共卖了15张票(资源不共享)如下: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Ticketextends Thread {
    private int tick = 5;
    public void run(){
        while(true) {
            if(tick>0)
            System.out.println("--卖票--"+tick--);
        }
    }
}
public class TicketDemo {
    public static void main(String args[]) {
        Ticket t1 = new Ticket();
        Ticket t2 = new Ticket();
        t1.start();
        t2.start();
    }
}

  Runnable接口实现卖票:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Ticket1implements Runnable {
    private int tick = 5;
    public void run(){
        while(true) {
            if(tick>0)
            System.out.println("--卖票--"+tick--);
        }
    }
}
public class TicketRunnableDemo {
    public static void main(String args[]) {
        Ticket1 t = new Ticket1();
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        t1.start();
        t2.start();
    }
}

  ★实现Runnable接口的Thread和MyThread ------代理设计模式

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
interface Runnable {
    public abstract void run();
}
 
class Threadimplements Runnable {
    public Thread(Runnable target) {
         
    }
    public void run() {
        //....
    }
    public void take() {
        System.out.println("--代理实际模式--");
    }
}
 
class MyThread001implements Runnable {
    public void run() {
        //....
    }
}
 
public class BorrowsDemo {
    public static void main(String args[]) {
        MyThread001 mt = new MyThread001();
        Thread t = new Thread(mt);
        t.take();
    }
}

  

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

用心-细心-专心-决心 学习就像爬大山,一步一步向前走 -态度决定高度-
原文地址:https://www.cnblogs.com/xianyou-liang/p/8503365.html