多线程进一步的理解------------线程的创建
生活随笔
收集整理的這篇文章主要介紹了
多线程进一步的理解------------线程的创建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class TestThread1 {public static void main(String[] args) {Thread thread = new Thread(){@Overridepublic void run () {while (true) {try {Thread.sleep(500);//讓它休息0.5秒} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "----------------");}}};thread.start();Thread thread1 = new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "----------------");}}});thread1.start();new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("4"+Thread.currentThread().getName() + "----------------");}}}){@Overridepublic void run() {while (true) {try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("3"+Thread.currentThread().getName() + "----------------");}}}.start();}
}
雖然創建多線程的方式有兩種但是我們一般常用
Thread thread1 = new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "----------------");}} }); thread1.start();因為這種方式更能體現面向對象的方式,把線程的創建和代碼部分分開了,還有一點必須要糾正的是,并不是說多線程一定會使項目快很多,還是那句話,沒有優秀的技術,只有適合的技術與不適合的技術。總結
以上是生活随笔為你收集整理的多线程进一步的理解------------线程的创建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装NodeJs运行环境
- 下一篇: 如何模拟弱网测试