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 修飾 ,在方法塊里面有調(diào)用了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 方法,直接調(diào)用的JVM C語言方法,才能啟動一個線程 ,所以只有我們有辦法啟動這個start0,就可以自己創(chuàng)建一個線程 但是他又是一個私有方法 好吧,先我們來東東手,以后再也不用老是在實現(xiàn)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();}}
}
?