java多线程------实现多线程两种方式
生活随笔
收集整理的這篇文章主要介紹了
java多线程------实现多线程两种方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//繼承實(shí)現(xiàn)多線程
package xian_cheng;public class Example01 {public static void main(String[] args) {// TODO Auto-generated method stubMyThread myThread=new MyThread();//創(chuàng)建線程對象myThread.start();//開啟進(jìn)程while (true ) {System.out.println("main()方法在運(yùn)行");}}
}
class MyThread extends Thread{ //繼承Thread類public void run(){while(true ){System.out.println("MyThread類的run在運(yùn)行");}}}//實(shí)現(xiàn)Runnable接口創(chuàng)建多進(jìn)程
package xian_cheng;public class Example03 {public static void main(String[] args) {// TODO Auto-generated method stub/*start(),啟動線程并執(zhí)行相應(yīng)的run方法* run():子線程要執(zhí)行的代碼放入run()方法中* currentThread(),靜態(tài)的,獲取當(dāng)前的線程* getName():獲取此線程的名字* setName():設(shè)置線程的名字* join()線程插隊(duì)* isAlive()線程是否還存活* sleep()線程睡眠* */MyThread2 myThread=new MyThread2();Thread thread=new Thread(myThread);thread.start();while (true ) {System.out.println("main()方法在運(yùn)行");}}
}
class MyThread2 implements Runnable{ //實(shí)現(xiàn)runnable接口//而且這個接口中只有一個run方法public void run(){while(true){System.out.println("MyThread類的run在運(yùn)行");}}
}
總結(jié)
以上是生活随笔為你收集整理的java多线程------实现多线程两种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jeecg下实现自动默认模糊查询
- 下一篇: MethodInvokingJobDet