java 线程难不难_java线程真的太难了!!!

作为一个码农,你知道如何启动一个java线程吗?

启动线程

public class PrintThread extendsThread {public voidrun() {

System.out.println("我是线程! 继承自Thread");

}public static voidmain(String args[]) {

(newPrintThread()).start();

}

}

亦或

public class HelloRunnable implementsRunnable {public voidrun() {

System.out.println("我也是一个线程,实现了接口");

}public static voidmain(String args[]) {

(new Thread(newHelloRunnable())).start();

}

}

又亦或周期性任务线程

/**

*

* @author dgm

* @describe "测试打印定时器"

* @date 2017年4月10日

*/

//注:public abstract class TimerTask implements Runnable

public class PrintTimerTask extendsTimerTask {privateString name;publicPrintTimerTask(String name) {super();this.name =name;

}

@Overridepublic voidrun() {if (System.currentTimeMillis( ) - scheduledExecutionTime( ) > 5000) {//让下一个任务执行

return;

}

System.out.println("周期性任务(好比每天早晨定闹钟)线程:"+ name +"***** 在 执行。。");

}

}

public classTimeTaskTest {public static voidmain(String[] args) {

Timer timer= newTimer();//设置3秒后启动任务

timer.schedule(new PrintTimerTask("name-0"), 3000);

PrintTimerTask secondTask= new PrintTimerTask("name-1");//1秒后启动任务,以后每隔3秒执行一次线程

timer.schedule(secondTask, 1000, 3000);

Date date= newDate();//以date为参数,指定某个时间点执行线程

timer.schedule(new PrintTimerTask("name-3"), newDate(

date.getTime()+ 5000));

}

}

b33e5768b1dfeec5bce86b4272f7a739.png

又亦或更时尚的调度器执行任务

/***

*@authordgm

* @describe ""

* @date 2020年4月10日*/

public class PrintScheduledExecutor implementsRunnable {privateString jobName;publicPrintScheduledExecutor() {

}publicPrintScheduledExecutor(String jobName) {this.jobName =jobName;

}

@Overridepublic voidrun() {

System.out.println("调度: "+ jobName + " 正在运行中!!!");

}

}

/***@authordgm

* @describe ""

* @date 2020年4月10日*/

public classScheduledThreadPoolTest {public static voidmain(String[] args) {

ScheduledExecutorService service= Executors.newScheduledThreadPool(5);long initialDelay = 1;long period = 1;//,固定频率,到期执行,从现在开始1秒钟之后,每隔1秒钟执行一次job1

service.scheduleAtFixedRate(new PrintScheduledExecutor("job1"),

initialDelay, period, TimeUnit.SECONDS);//频率不一定固定,从现在开始2秒钟之后,每隔2秒钟执行一次job2

service.scheduleWithFixedDelay(new PrintScheduledExecutor("job2"),

initialDelay, period, TimeUnit.SECONDS);

}

}

bf8ce7421b69feed0db20c3df4a0813a.png

虽然·运行良好,不建议 Executors.newScheduledThreadPool(5);,最终还是希望用这个参数明确的的方式构造线程池

/*** Creates a thread pool that can schedule commands to run after a

* given delay, or to execute periodically.

*@paramcorePoolSize the number of threads to keep in the pool,

* even if they are idle

*@returna newly created scheduled thread pool

*@throwsIllegalArgumentException if {@codecorePoolSize < 0}*/

public static ScheduledExecutorService newScheduledThreadPool(intcorePoolSize) {return newScheduledThreadPoolExecutor(corePoolSize);

}/*** Creates a new {@codeScheduledThreadPoolExecutor} with the

* given core pool size.

*

*@paramcorePoolSize the number of threads to keep in the pool, even

* if they are idle, unless {@codeallowCoreThreadTimeOut} is set

*@throwsIllegalArgumentException if {@codecorePoolSize < 0}*/

public ScheduledThreadPoolExecutor(intcorePoolSize) {super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,newDelayedWorkQueue());

}public ThreadPoolExecutor(intcorePoolSize,intmaximumPoolSize,longkeepAliveTime,

TimeUnit unit,

BlockingQueueworkQueue,

ThreadFactory threadFactory,

RejectedExecutionHandler handler) {if (corePoolSize < 0 ||maximumPoolSize<= 0 ||maximumPoolSize< corePoolSize ||keepAliveTime< 0)throw newIllegalArgumentException();if (workQueue == null || threadFactory == null || handler == null)throw newNullPointerException();this.corePoolSize =corePoolSize;this.maximumPoolSize =maximumPoolSize;this.workQueue =workQueue;this.keepAliveTime =unit.toNanos(keepAliveTime);this.threadFactory =threadFactory;this.handler =handler;

}

17953ee3913e3760b83c6449ef0c4c3e.png

最后一种方式参数清晰明了

程序虽然执行了,不过很纳闷,  start()如何启动线程的。。。。。。

0a6ae3c40009e2ae4344de7c3156cd3b.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

fafaaa038300011f8c3f75c14599b8cf.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

其他类还有不少native方法强大无比,例如

966c9f9ba80f3789f51772592ee99111.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

85baa289af35a8f6d076c098d107a519.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

-------------------------------------------------------------------------------------------------------------------------------------------------------

在想往下看就要有C& C++,系统方面的知识了 ,毕竟jvm是个托管的虚拟机,于java码农屏蔽了很多底层细节,底层怎么创建、调度、监视、执行线程,不是java语言多强大,确切的说而是底层很强大。

80cdd30b3aa7cf440d51bf8a367347fc.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

0357aa455d134404e92d5ebe15917fc5.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

d4ae9d36a8af609154932531b97c3684.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

439da75ebceeeed375588b30d696dcc8.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

1470220b0e0c6ab88c7a117462853c96.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==​ 9a2e154ebf6b8c38d7d2d6a7fcd3d5e5.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

dc8228b040173e913d2c3cfb78375b50.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

c22e7c7aa25c077a36c68d4c3eaa100c.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

c88674f388e7e5b403764a192cfdd001.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==​ 4844d45bebca732fdb2d4fbeef2cf9e6.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

f8de594da221fd6d1ce9758869e2e7da.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

0d072241a16d65af0f8862f2634d071d.pngwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

小结略,以后补

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值