Runnable vs thread

zhaozj2021-02-16  52

Everyone knows that when we write multi-thread, you often want to implement a thread to rewrite the start () method by overwriting Run (). In many cases, we may also use the Implements Runnable to implement a thread, they What's the difference?

Let's look at an example: Suppose we need to implement an object, check if the port of a server is still active, we write an object, as follows:

Package com.college;

Import java.net.url; import java.net.httpurlConnection; import java.io. *; import java.net. *;

Public class sendmsg extends thread {

Public final static int sysisok = 1; public final static int sysisdown = -1; public final static int sysisoutoft final static int sysisexception = -3;

String testURL; private URL URL;

Boolean NeedStop = false; / ** * Read if you need to stop the main thread * @return stop sign * / public synchronized boolean getNeedStop () {Return NeedStop;

} / ** * Set whether you need to stop the main thread, if you call the START () method of the change thread to the external call, you can stop * @Param B stop sign * / public synchronized void setneedStop (Boolean B) { NeedStop = B;

}

/ ** * Constructor * @Param TestURL To detect URL * / public sendmsg (string testURL) {this.testurl = testURL;

} / ** * status of the detected system * / private int syste = 0;

/ ** * Set the status of the detected system or use to clear the reset process * @Param State is about to set. * / Public synchronized void setsysstate (int 2) {system.out.println ("set state =" state); this.sysstate = state;}

/ ** * Returns the current status of the detected system * @return's current status 0: No response, 1: Normal, -1: Machine, -3: Other errors * / public synchronized int getsysstate () {Return sysstate;}

Public void run () {new testurlthread (this) .start (); synchronized (this) {try {this.wait (30 * 1000);} catch (interruptedException E) {} if (getsysState ) == sysisok) {system.out.println ("all normal");} else} {system.out.println ("System");} else} else} else == sysisoutoftime) {system.out.println ("System Long-term No Resist");} else {system.out.println ("Other errors in the system");}

}

}

}

Public static void main (string [] args) {new sendmsg ("http://www.163.com") .Start ();

/ ************************************************** ********************* * Internal classes start ********************** *********************************************************** /

Class Testurlthread Extends thread {

Sendmsg sendsmg = null; testurlthread (sendmsg send) {sendsmg = send;}

/ ** * As a Thread inherited, cover the parent RUN method * / public void run () {

// Reset setsysState (Sendsmg.sysisoutoftime); httpurlconnection httpcon = null; string results;

Try {url = new url;

httpCon = (HttpURLConnection) url.openConnection (); BufferedReader in = new BufferedReader (new InputStreamReader (httpCon.getInputStream ())); results = in.readLine (); httpCon.disconnect (); setSysState (sendSmg.SYSISOK); / ** * If you don't want to immediately inform the Sendsmg thread, the synchronous body can be removed! * / Synchronized (sendSMG) {sendsmg.notify (); system.out.println ("notify sendsmg");}}} Catch (ConnecTexception EX) {// If the connection exception is established, the system is turned off setsysState (Sendsmg.sisdown) ; / ** * If you don't want to immediately inform the Sendsmg thread, the synchronous body can be removed! * / Synchronized (sendSMG) {seundsmg.notify ();}} catch (exception ex) {

SetsysState (Sendsmg.sysiSException); / ** * If you don't want to immediately inform the Sendsmg thread, you can remove the synchronous body! * / Synchronized (sendsmg) {seundsmg.notify ();}}

Finally {if (httpcon! = null) {httpcon.disconnect (); httpcon = null;}}

}

}

/ ************************************************** ******************************************************************************************************************** *********************************************************** /

}

In this example, we use TestURLThread to test the 80-port of the 163 website. Once there is a result, the Wait () method of the notify () sendMSG thread is not allowed to make him silly, and now we can use Testurlthread. Do not heever Thread inherit, but expand the runnable interface to achieve, in SendMsg

New Testurlthread (this) .Run (); Run () method to call Testurlthread? the answer is negative

Because, once replaced with runnable, sendmsg.notify () will not be able to wake up WAIT (), which can only rely on this.wait (30 * 1000);

This is the conclusion that I have found after I found out, I hope the netizen discuss it, does it make sense, or what is the reason?

转载请注明原文地址:https://www.9cbs.com/read-15388.html

New Post(0)