JAVA thread Learning (1)

Source: Internet
Author: User

JAVA thread Learning (1)
Package com;


Public class Actor extends Thread {
Private int count;
// Running Method
Public void run (){
System. out. println (getName () + "is an actor ");
System. out. println (getName () + "" + (++ count ));
System. out. println (getName () + "End of performance ");
}


// Observe how many run methods are called.
Public static void main (String [] args ){
Actor ac = new Actor ();
Ac. setName ("Mr Thread ");
Ac. start ();

}

}



// ================================================ ========================================== //

Infinite Loop



Package com;


Public class Actor extends Thread {
Private int count;
Private boolean keepRunning = true;
// Running Method
Public void run (){
System. out. println (getName () + "is an actor ");
While (keepRunning)
{
System. out. println (getName () + "" + (++ count ));
}
System. out. println (getName () + "End of performance ");
}


// Observe how many run methods are called.
Public static void main (String [] args ){
Actor ac = new Actor ();
Ac. setName ("Mr Thread ");
Ac. start ();

}

}




// ================================================ ====================== //

Added the judgment 100 times



Package com;


Public class Actor extends Thread {
Private int count;
Private boolean keepRunning = true;
// Running Method
Public void run (){
System. out. println (getName () + "is an actor ");
While (keepRunning)
{
System. out. println (getName () + "" + (++ count ));
If (count = 100)
{

KeepRunning = false;
}
}
System. out. println (getName () + "End of performance ");
}


// Observe how many run methods are called.
Public static void main (String [] args ){
Actor ac = new Actor ();
Ac. setName ("Mr Thread ");
Ac. start ();

}

}



// ================================================ ============ //

Add sleep

Every 10 times, one second of rest



Package com;


Public class Actor extends Thread {
Private int count;
Private boolean keepRunning = true;
// Running Method
Public void run (){
System. out. println (getName () + "is an actor ");
While (keepRunning)
{
System. out. println (getName () + "" + (++ count ));
If (count = 100)
{

KeepRunning = false;
}
If (count % 10 = 0)
{
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

}
}
System. out. println (getName () + "End of performance ");
}


// Observe how many run methods are called.
Public static void main (String [] args ){
Actor ac = new Actor ();
Ac. setName ("Mr Thread ");
Ac. start ();

}

}



// ================================================ ================ //

Implement the runnable interface

Alternate implementation of two threads

Package com;


Public class Actor extends Thread {
Private int count;
Private boolean keepRunning = true;
// Running Method
Public void run (){
System. out. println (getName () + "is an actor ");
While (keepRunning)
{
System. out. println (getName () + "" + (++ count ));
If (count = 100)
{

KeepRunning = false;
}
If (count % 10 = 0)
{
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

}
}
System. out. println (getName () + "End of performance ");
}


// Observe how many run methods are called.
Public static void main (String [] args ){
Actor ac = new Actor ();
Ac. setName ("Mr Thread ");
Ac. start ();

Thread actressThread = new Thread (new Actress (), "Ms Runnable ");
ActressThread. start ();

}

}




Class Actress implements Runnable {


Private int count;
Private boolean keepRunning = true;
// Running Method
Public void run (){
System. out. println (Thread. currentThread (). getName () + "is an actor ");
While (keepRunning)
{
System. out. println (Thread. currentThread (). getName () + "" + (++ count ));
If (count = 100)
{

KeepRunning = false;
}
If (count % 10 = 0)
{
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

}
}
System. out. println (Thread. currentThread (). getName () + "End of performance ");
}




}

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.