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 ");
}
}