Java实现线程的两种方式

一、什么是线程?
摘自官方API:A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.这句话的意思是说:线程是程序中的一个执行线程。 Java虚拟机允许应用程序同时运行多个执行线程。
注:关于线程的知识请自行查阅资料。
二、线程的实现方式
(1)扩展(使用extens)Thread类并重写run()方法。
class PrimeThread extends Thread {
long minPrime;
public PrimeThread (long minPrime) {
this.PrimeThread = PrimeThread ;
}
public void run(){
System.out.println(“扩展Thread类并重写run()方法”);
}
}

创建一个新的线程并启动:
PrimeThread pt = new PrimeThread(120);
pt.start();
(2)实现(implements) Runnable接口
class RunThread implements Runnable {
long minRunTime;
public RunThread (long minRunTime){
this.minRunTime = minRunTime;
}
}
创建一个新的线程并启动:
RunThread rt = new RunThread(110);
new Thread(rt).start();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值