java常用多线程示例_Java多线程-2-常用的实现多线程的两种方式

Thread和Runnable

1、简介

Runnable是一个接口,该接口中只包含了一个run()方法。它的定义为:

public interfaceRunnable

{public abstract voidrun()

}

Runnable的作用为实现多线程。我们可以定义一个类A,实现Runnable接口;然后通过new Thread(new A())方式新建线程

Thread是一个类,它背身就实现了Runnable接口。它的声明为:

public class Thread implements Runnable{}

Thread类的作用,也是实现多线程

2、Thread和Runnable的异同点

相同点:都是“多线程”的实现方式

不同点:(1)、Thread是类,而Runnable是接口;Thread本身是实现了Runnable接口的类。而一个类只能继承一个父类,却可以实现多个接口,因此Runnable具有更好的扩展性;(2)、Runnable还可以用于“资源共享”,即,多个线程都是基于某一个Runnable对象建立的,它们会共享Runnable对象上的资源。

3、Thread的多线程示例

class MyThread extendsThread

{private int ticket = 10;public voidrun()

{for(int i = 0;i < 20;i++)

{if(this.ticket > 0)

System.out.println(this.getName()+" sells a ticket. Before this we have:"+this.ticket--);

}

}

}public classproblem1

{public static voidmain(String[] args)

{

MyThread t1= newMyThread();

MyThread t2= newMyThread();

MyThread t3= newMyThread();

t1.start();

t2.start();

t3.start();

}

}

运行结果为:

Thread-1 sells a ticket. Before this we have:10Thread-0 sells a ticket. Before this we have:10Thread-0 sells a ticket. Before this we have:9Thread-0 sells a ticket. Before this we have:8Thread-0 sells a ticket. Before this we have:7Thread-0 sells a ticket. Before this we have:6Thread-0 sells a ticket. Before this we have:5Thread-0 sells a ticket. Before this we have:4Thread-0 sells a ticket. Before this we have:3Thread-0 sells a ticket. Before this we have:2Thread-2 sells a ticket. Before this we have:10Thread-2 sells a ticket. Before this we have:9Thread-2 sells a ticket. Before this we have:8Thread-2 sells a ticket. Before this we have:7Thread-2 sells a ticket. Before this we have:6Thread-2 sells a ticket. Before this we have:5Thread-2 sells a ticket. Before this we have:4Thread-2 sells a ticket. Before this we have:3Thread-2 sells a ticket. Before this we have:2Thread-2 sells a ticket. Before this we have:1Thread-0 sells a ticket. Before this we have:1Thread-1 sells a ticket. Before this we have:9Thread-1 sells a ticket. Before this we have:8Thread-1 sells a ticket. Before this we have:7Thread-1 sells a ticket. Before this we have:6Thread-1 sells a ticket. Before this we have:5Thread-1 sells a ticket. Before this we have:4Thread-1 sells a ticket. Before this we have:3Thread-1 sells a ticket. Before this we have:2Thread-1 sells a ticket. Before this we have:1

结果说明:

(1)、MyThread类继承于Thread类,它是自定义的线程。每个MyThread会卖出10张票

(2)、主线程main创建并启动了3个MyThread子线程。每个子线程都各自卖出了10张票

4、Runnable的多线程示例

class MyThread implementsRunnable

{private int ticket = 10;public voidrun()

{for(int i = 0;i < 20;i++)

{if(this.ticket > 0)

System.out.println(Thread.currentThread().getName()+ " sells a ticket. Before this we have:"+this.ticket--);

}

}

}public classproblem1

{public static voidmain(String[] args)

{

MyThread mt= newMyThread();

Thread t1= newThread(mt);

Thread t2= newThread(mt);

Thread t3= newThread(mt);

t1.start();

t2.start();

t3.start();

}

}

运行结果为:

Thread-0 sells a ticket. Before this we have:10Thread-0 sells a ticket. Before this we have:7Thread-2 sells a ticket. Before this we have:8Thread-2 sells a ticket. Before this we have:5Thread-1 sells a ticket. Before this we have:9Thread-2 sells a ticket. Before this we have:4Thread-0 sells a ticket. Before this we have:6Thread-2 sells a ticket. Before this we have:2Thread-1 sells a ticket. Before this we have:3Thread-0 sells a ticket. Before this we have:1

结果说明:

(1)、和上面的MyThread类继承于Thread类不同,这里的MyThread实现了Runnable接口

(2)、主线程main创建并启动了三个子线程,而且这三个子线程都是基于"mt"这个Runnable对象创建的。运行结果是这三个子线程一共卖出了10张票,这说明它们是共享资源的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值