Java 多线程:extends Thread Class & implements Runnable Interface

来源:互联网 发布:win10卸载软件 编辑:程序博客网 时间:2024/06/12 01:47
1. extends Thread Class
2. implements Runnable Interface

3. Runnable is more flexible than Thread


[ extends Thread Class ]

 - extends Thread Class
 public class DemoClass extends Thread(){    //class definition }

 - Override "public void run(){}" method

 @Override public void run(){    //function definition }


 - using .start() function to run Thread
 DemoClass d1 = new DemoClass(); d1.start(); DemoClass d2 = new DemoClass(); d2.start();


 
 Example:
public class MyThread2 extends Thread{/* Since java only support extends only 1 class, * MyThread2 can not act as other class's subclass*/String name;int time_pause;MyThread2(String name, int time_pause){this.name = name;this.time_pause = time_pause;}/* * override run() from super class */@Overridepublic void run(){while(true){System.out.println(name);try {Thread.sleep(time_pause);} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args){/*directly use the Class "MyThread2" we defined before*/MyThread2 mt1 = new MyThread2("fast",1000);mt1.start();MyThread2 mt2 = new MyThread2("slow", 3000);mt2.start();}}


 

[ implements Runnable Interface ] 

1. implement Runnable Interface
public Demo implements Runnable {}


2. Override public void run() method
@Overridepublic void run() {}


3. take advantage of Thread(Runnable), initialize a Thread object
public class Demo implements Runnable...Thread t = new Thread(new Demo());t.start();Demo m = new Demo();Thread t2 = new Thread(m);t2.start();




Example:
public class MyThread implements Runnable{/* * Implements Runnable is more preferred. * A class can implement multiple interfaces, * but can only extends single calss*/int pause_time;String name;MyThread(int pause_time, String name){this.pause_time=pause_time;this.name = name;}@Overridepublic void run() {while(true){System.out.println(name+":"+new Date(System.currentTimeMillis()));try {Thread.sleep(pause_time);} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args) {/* * can not create the class directly, * initialize the class using Thread class*/Thread t1 = new Thread(new MyThread(1000, "fast"));t1.start();Thread t2 = new Thread(new MyThread(3000, "slow"));t2.start();}}


Runnable Interface 只有public void run()一个接口,没有start()接口。
而Thread类有Constructor:public Thread(Runnable),
因此,实现Runnable的类需要借助Thread的该Constructor来实例化,才能使用start()。

[ Runnable is more flexible than Thread ]

Thread是实现Runnable的一个子类。
在实际开发中一个多线程的操作很少使用Thread类,而是通过Runnable接口完成。

在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable接口相比
继承Thread类有如下好处:
1. 避免点继承的局限(only support extends single class),一个类可以继承多个接口。
e.g.
public class Demo1 extends Thread {} //extends singel classpublic class Demo2 implements Runnable, OtherInterface {} //implements multiple interfaces




2. 适合于资源的共享
e.g.
...Demo1 extends Thread...Demo1 d1 = new Demo1();d1.start();Demo1 d2 = new Demo1();d2.start();//d1 d2 are seperate threads, share no data.


...Demo2 implements Runnable...
Demo2 d = new Demo2();Thread d1 = new Thread(d);d1.start();Thread d2 = new Thread(d);d2.start();// d1 & d2 using same d, they share d.

ref:

blog.csdn.net/wwww1988600/article/details/7309070


0 0
原创粉丝点击