Java supports multiple threads working simultaneously!

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.