java多線程編程之使用runnable接口創(chuàng)建線程
1.將實現(xiàn)Runnable接口的類實例化。
2.建立一個Thread對象,并將第一步實例化后的對象作為參數(shù)傳入Thread類的構造方法。
最后通過Thread類的start方法建立線程。
下面的代碼演示了如何使用Runnable接口來創(chuàng)建線程:
package mythread;
public class MyRunnable implements Runnable
{
public void run()
{
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args)
{
MyRunnable t1 = new MyRunnable();
MyRunnable t2 = new MyRunnable();
Thread thread1 = new Thread(t1, "MyThread1");
Thread thread2 = new Thread(t2);
thread2.setName("MyThread2");
thread1.start();
thread2.start();
}
}
[/code]
上面代碼的運行結果如下:
MyThread1
MyThread2
相關文章
Java中system.exit(0) 和 system.exit(1)區(qū)別
本文主要介紹了Java中system.exit(0) 和 system.exit(1)區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05
解決springboot bean中大寫的字段返回變成小寫的問題
這篇文章主要介紹了解決springboot bean中大寫的字段返回變成小寫的問題,具有很好的參考價值希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池無效問題)
這篇文章主要介紹了springboot2整合redis使用lettuce連接池(解決lettuce連接池無效問題),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12

