public static void Main (string[] args) {Runnable status_runnable=generatestatus (); Runnable Session_runnable=mythread (session); Thread Statusthread=thread (status_runnable);//thread with Task thread session_thread=thread (session_runnable); Statusthread.start (); Session_thread.start ();
}
The implementation of the two classes is as follows, the first of which is generatestatus
import java.util.random;public class generatestatus implements runnable{ private String temperature; private String free_Space; public generatestatus () { /* constructor method */ } /* program running in thread */ public void run () {//here because implementing the interface this must implement random random=new random (); while (True) { temperature = ((Integer) ( Random.nextint (+20)). ToString (); Free_space= ((Integer) (Random.nextint (+1)). ToString () + "G"; statusmap.Put ("Temperature", temperature); Statusmap.put ("Free_space", Free_space);  SYSTEM.OUT.PRINTLN ("test"); try { Thread.Sleep (; } catch ) ( Interruptedexception e) { e.printstacktrace (); } } }}
The second class of Mythread
Javax.websocket.session;java.io.ioexception; mythread runnable { string ; string ; session ; mythread (session session) IOException { .=session; } run () { () { { thread. (); } (InterruptedException e ) { E.printstacktrace (); } system. println (); =statusmap. (); =statusmap. (); { .getbasicremote (). SendText (); .getbasicremote (). SendText (); } (IOException e) { E.printstacktrace (); } } }}
PS: If you only need one thread, you can directly create a thread to inherit the thread, which is called directly in the main function.
Thread Statusthread=new MyThread (session); Statusthread.start ();//This will do. But you can't run multiple threads at the same time!
Java supports multiple threads working simultaneously!