A preliminary study of Java multithreading

Source: Internet
Author: User
Tags constructor sleep thread class

Import java.io.*;


//Multithreaded programming


public class multithread


{


public static void Main (String args[])


{


System.out.println ("I am the main thread!");


//Below Create thread instance Thread1


threaduseextends thread1=new threaduseextends ();


//When creating thread2 to implement the Runnable interface Thhreaduserunnable class instance as parameter


thread thread2=new Thread (new threaduserunnable (), "Secondthread");


Thread1.start ()//start thread thread1 put it in a ready state


//thread1.setpriority (6);//Set THREAD1 priority to 6


//Priority will determine when the CPU is empty, the ready-state thread who first occupies the CPU starts to run


//Priority range 1 to 10,min_priority,max_priority,norm_paiority


//New thread inheritance creates her parent thread priority, and the parent thread usually has a normal priority, that is, 5norm_priority


System.out.println ("Main thread will hang for 7 seconds!");


Try


{


Thread.Sleep (7000);/main thread hangs 7 seconds


}


catch (interruptedexception e)


{


Return


}


System.out.println ("Back to the main thread!");


if (thread1.isalive ())


{


thread1.stop ()//If Thread1 still exists, kill him


System.out.println ("Thread1 sleep too long, the main thread killed the thread1!");


}


Else


System.out.println ("Main thread did not find Thread1,thread1 awake sequence execution ended!");


Thread2.start ()//Start thread2


System.out.println ("Main thread will hang up again for 7 seconds!");


Try


{


Thread.Sleep (7000);/main thread hangs 7 seconds


}


catch (interruptedexception e)


{


return;


}


System.out.println ("Back to the main thread!");


if (thread2.isalive ())


{


thread2.stop ()//If thread2 still exists, kill him


System.out.println ("Thread2 sleep too long, the main thread killed the thread2!");


}


Else


System.out.println ("Main thread did not find thread2,thread2 awake sequence execution ended!");


System.out.println ("Press any key at end of program to continue!");


Try


{


System.in.read ();


}


catch (IOException e)


{


System.out.println (e.tostring ());


}


}//main


}//multithread


class Threaduseextends extends Thread


//By inheriting the thread class and implementing its abstract method run ()


//When appropriate, create an instance of this thread subclass to implement multithreaded mechanisms


//A thread starts (or is in a ready state) once it gets the CPU will automatically call its run () method


{


threaduseextends () {}//constructor


public void Run ()


{


System.out.println ("I am the thread instance of the thread subclass!");


System.out.println ("I will hang up for 10 seconds!");


System.out.println ("Back to the main thread, please wait a moment, just hang up the main thread may not wake up!") ");


Try


{


sleep (10000);//Hang up 5 seconds


}


catch (interruptedexception e)


{


return;


}


If the run () method is finished sequentially, the thread will automatically end without being killed by the main thread


//But if the sleep time is too long, the thread survives and may be killed by Stop ()


}


}


class Threaduserunnable implements Runnable


///By implementing the Run () method in the Runnable interface, and then using this to implement the class of the Run () method


//Create thread instances for parameters


{


//thread thread2=new Thread (this);


//Creates a thread instance of the threads class for the parameter with this class that implements the run () method in the Runnable interface


threaduserunnable () {}//constructor


public void Run ()


{


System.out.println ("I am the thread instance of the thread class and take arguments for the class that implements the Runnable interface!");


System.out.println ("I will hang up for 1 seconds!");


System.out.println ("Back to the main thread, please wait a moment, just hang up the main thread may not wake up!") ");


Try


{


Thread.Sleep (1000);//Hang up for 5 seconds


}


catch (interruptedexception e)


{


return;


}


//If the run () method is finished sequentially, the thread will automatically end without being killed by the main thread


//But if the sleep time is too long, the thread survives and may be killed by Stop ()


}


}


//The program can make modifications such as changing the sleep time or Priority setpriority ()

Related Article

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.