标签:类继承 int test The nts 开启 div 两种 运行
大纲:
一、java创建线程--继承Thead类
创建一个类继承Thead类,并重写run方法。
class Test { public static void main(String[] args) { System.out.println("当前线程:"+Thread.currentThread()); new TestThread().start(); new TestThread().start(); } static class TestThread extends Thread { @Override public void run() { System.out.println("当前线程:"+Thread.currentThread()); } } } /**
运行结果: 当前线程:Thread[main,5,main] 当前线程:Thread[Thread-0,5,main] 当前线程:Thread[Thread-1,5,main] */
二、java创建线程--实现Runnable接口
创建一个类实现Runnable接口,并重写run方法。
class Test { public static void main(String[] args) { System.out.println("当前线程:"+Thread.currentThread()); new Thread(new TestThread()).start(); new Thread(new TestThread()).start(); } static class TestThread implements Runnable { @Override public void run() { System.out.println("当前线程:"+Thread.currentThread()); } } } /** 运行结果: 当前线程:Thread[main,5,main] 当前线程:Thread[Thread-0,5,main] 当前线程:Thread[Thread-1,5,main] */
小结:
标签:类继承 int test The nts 开启 div 两种 运行
原文地址:https://www.cnblogs.com/liuboyuan/p/9945652.html