日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

再谈 Java中Runnable和Thread的区别

發布時間:2024/4/14 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 再谈 Java中Runnable和Thread的区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在面試中老有面試官問這個問題,個人覺得這個問題問的沒有技術,一個死記硬背就能回答的很好。但是更深的回答是什么了,那就是直接回答源碼吧。 thread類實現了runnable 接口 ,Runnable就是一個借口 ,只能我們去實現了才能用 對吧,不管是普通類 還是匿名內部類 ,最大的區別是我們的自己的實現類 沒有辦法啟動線程,還是要借助于thread

結果就出現了線程的實現方法

package test8;/*** 方式一* @author suifeng**/ class myThread extends Thread{@Overridepublic void run() {// TODO Auto-generated method stubSystem.out.println(Thread.currentThread());} }/*** 方式二* @author suifeng**/ class myRunable implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stubSystem.out.println(Thread.currentThread());} } public class DT {public static void main(String[] args) {// TODO Auto-generated method stubnew myThread().start();new Thread(new myRunable()).start();}}
但是不管是什么方法都需要 thread類的start 方法告訴JVM 啟動線程 ,start()方法被?synchronized 修飾 ,在方法塊里面有調用了start0() 方法 public synchronized void start() {/*** This method is not invoked for the main method thread or "system"* group threads created/set up by the VM. Any new functionality added* to this method in the future may have to also be added to the VM.** A zero status value corresponds to state "NEW".*/if (threadStatus != 0)throw new IllegalThreadStateException();/* Notify the group that this thread is about to be started* so that it can be added to the group's list of threads* and the group's unstarted count can be decremented. */group.add(this);boolean started = false;try {start0();started = true;} finally {try {if (!started) {group.threadStartFailed(this);}} catch (Throwable ignore) {/* do nothing. If start0 threw a Throwable thenit will be passed up the call stack */}}}

private native void start0();
start0方法是一個native 方法,直接調用的JVM C語言方法,才能啟動一個線程 ,所以只有我們有辦法啟動這個start0,就可以自己創建一個線程 但是他又是一個私有方法 好吧,先我們來東東手,以后再也不用老是在實現runnable和在start()了


package test8;import java.lang.Thread.State; import java.lang.Thread.UncaughtExceptionHandler;interface Work {void work(); }/*** 裝飾模式 * @author suifeng**/ class Mthread implements Runnable {Thread dt;Work w;public Mthread(Work w) {dt = new Thread(this);this.w = w;dt.start();}public int hashCode() {return dt.hashCode();}public boolean equals(Object obj) {return dt.equals(obj);}public void start() {dt.start();}// ...public void run() {w.work();}public final void stop() {dt.stop();}public final void stop(Throwable obj) {dt.stop(obj);}public void interrupt() {dt.interrupt();}public boolean isInterrupted() {return dt.isInterrupted();}public void destroy() {dt.destroy();}public final boolean isAlive() {return dt.isAlive();}public final void suspend() {dt.suspend();}public final void resume() {dt.resume();}public final void setPriority(int newPriority) {dt.setPriority(newPriority);}public final int getPriority() {return dt.getPriority();}public final void setName(String name) {dt.setName(name);}public final String getName() {return dt.getName();}public final ThreadGroup getThreadGroup() {return dt.getThreadGroup();}public int countStackFrames() {return dt.countStackFrames();}public final void join(long millis) throws InterruptedException {dt.join(millis);}public final void join(long millis, int nanos) throws InterruptedException {dt.join(millis, nanos);}public final void join() throws InterruptedException {dt.join();}public final void setDaemon(boolean on) {dt.setDaemon(on);}public final boolean isDaemon() {return dt.isDaemon();}public final void checkAccess() {dt.checkAccess();}public String toString() {return dt.toString();}public ClassLoader getContextClassLoader() {return dt.getContextClassLoader();}public void setContextClassLoader(ClassLoader cl) {dt.setContextClassLoader(cl);}public StackTraceElement[] getStackTrace() {return dt.getStackTrace();}public long getId() {return dt.getId();}public State getState() {return dt.getState();}public UncaughtExceptionHandler getUncaughtExceptionHandler() {return dt.getUncaughtExceptionHandler();}public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {dt.setUncaughtExceptionHandler(eh);} }public class DthredTest {public static void main(String[] args) {new Mthread(new Work() {@Overridepublic void work() {System.out.println(Thread.currentThread());}});new Mthread(new Work() {@Overridepublic void work() {// TODO Auto-generated method stubSystem.out.println(Thread.currentThread());}});while (Thread.activeCount() > 1) {Thread.yield();}} }
?




總結

以上是生活随笔為你收集整理的再谈 Java中Runnable和Thread的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。