通过内部类创建线程四大方法

it2023-08-14  67

通过内部类创建线程

静态内部类 局部内部类 匿名内部类 Lambda

public class DemoThread007 { //静态内部类 static class Inner extends Thread{ @Override public void run(){ for(int i = 0;i<20;i++){ System.out.println("一边抽烟...."); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { new Inner().start(); //局部内部类 class Inner2 implements Runnable{ @Override public void run() { for(int i = 0;i<20;i++){ System.out.println("一边打游戏...."); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } new Thread(new Inner2()).start(); //匿名内部类 new Thread(new Runnable() { @Override public void run() { for (int i =1;i<=20;i++){ System.out.println("一边喝酒...."); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); //lambda 表达式 new Thread(()->{ for (int i=1;i<20;i++){ System.out.println("一边唱歌"); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } }
最新回复(0)