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

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
	 */
	@Override
	public 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
@Override
public 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;
	}


	@Override
	public 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 class
public 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值