<>lambda的推导:

* 是为了避免匿名内部类定义过多。 public class lambdaTest { public static void main(String[]
args) { // TODO Auto-generated method stub new Thread(()->
//括号中写的是参数,如果是一个参数则可以省略括号直接写参数 System.out.println("一边学习")).start();//一行代码可以省略
new Thread(()->{ System.out.println("一边蒙"); }).start(); } }
<>守护线程

是为用户线程服务的,JVM停止不用等待守护线程执行完毕。

* 默认:用户线程
* JVM等待用户线程执行完毕才会停止。 public class DeamonTest { public static void main(String[
] args) { // TODO Auto-generated method stub God god=new God(); Yous you=new
Yous(); Thread t=new Thread(god); t.setDaemon(true);//将God设置为守护线程 t.start();
//用户线程执行完毕后,不用等待守护线程的执行完毕,程序就可能停止。 new Thread(new Yous()).start(); } } class
Yous implements Runnable{ @Override public void run() { for(int i=1;i<=365*100;i
++) { System.out.println("happy life..."); } System.out.println("espire!"); } }
class God implements Runnable{ @Override public void run() { for(int i=1;i<=365*
100000;i++) { System.out.println("blessing...."); } } }
<>利用龟兔赛跑的游戏理解线程
public class Racer implements Runnable{ private String winner; @Override public
void run() { for(int steps = 1;steps<=100;steps++) { if(Thread.currentThread().
getName().equals("rabbit")&&(steps%10)==0) { try { Thread.sleep(100); } catch (
InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace()
; } } System.out.println(Thread.currentThread().getName()+"--->"+steps); if(
gameOver(steps)) { break; } } } private boolean gameOver(int steps) { if(winner
!=null) { return true; }else { if(steps==100) { winner=Thread.currentThread().
getName(); System.out.println("winner====>"+winner); return true; } } return
false; } public static void main(String[] args) { // TODO Auto-generated method
stub Racer racer=new Racer(); new Thread(racer,"rabbit").start(); new Thread(
racer,"tortoise").start(); } }

技术
今日推荐
PPT
阅读数 121
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信