public  class  DemoThread  { public  static  void  main ( String [ ]  args)  { MyThread  myThread =  new  MyThread ( ) ; System . out. println ( "run方法没有启动..." ) ; myThread. run ( ) ; System . out. println ( "run方法启动完成..." ) ; System . out. println ( "run方法没有启动..." ) ; myThread. start ( ) ;  System . out. println ( "run方法启动完成..." ) ; } 
} 
class  MyThread  extends  Thread { @Override public  void  run ( )  { for  ( int  i =  0 ;  i <  100 ;  i++ )  { try  { Thread . sleep ( 100 ) ; }  catch  ( InterruptedException  e)  { throw  new  RuntimeException ( e) ; } System . out. println ( "这是创建的一个新线程,定义了其中的run方法..." ) ; } } 
} 
public  class  DemoThreadName  { public  static  void  main ( String [ ]  args)  { 
Thread  thread =  Thread . currentThread ( ) ;  System . out. println ( "当前线程名称:"  +  thread. getName ( ) ) ; System . out. println ( "当前线程ID:"  +  thread. getId ( ) ) ; System . out. println ( "当前线程状态:"  +  thread. getState ( ) ) ;   System . out. println ( "当前线程优先权:"  +  thread. getPriority ( ) ) ; 
new  ThreadName ( ) . start ( ) ;   new  ThreadName ( ) . start ( ) ;   new  ThreadName ( "张三" ) . start ( ) ; new  ThreadName ( "李四" ) . start ( ) ; ThreadName  threadName =  new  ThreadName ( ) ; threadName. setName ( "王五" ) ; threadName. start ( ) ; } static  class  ThreadName  extends  Thread  { public  ThreadName ( )  { } 
public  ThreadName ( String  name)  { super ( name) ; } @Override public  void  run ( )  { while  ( true )  { 
System . out. println ( "当前线程名称:"  +  this . getName ( ) ) ; System . out. println ( "当前线程ID:"  +  this . getId ( ) ) ; System . out. println ( "当前线程状态:"  +  this . getState ( ) ) ; System . out. println ( "当前线程优先权:"  +  this . getPriority ( ) ) ; System . out. println ( "这是自定义类中的run方法执行代码..." ) ; try  { Thread . sleep ( 1000 ) ; }  catch  ( InterruptedException  e)  { throw  new  RuntimeException ( e) ; } } } } } 
public  class  DemoPriority  { public  static  void  main ( String [ ]  args)  { PriorityThread  thread1 =  new  PriorityThread ( "张三" ) ; thread1. setPriority ( 1 ) ; PriorityThread  thread2 =  new  PriorityThread ( "李四" ) ; thread2. setPriority ( 2 ) ; PriorityThread  thread3 =  new  PriorityThread ( "王五" ) ; thread3. setPriority ( 3 ) ; thread2. start ( ) ; thread1. start ( ) ; thread3. start ( ) ; } static  class  PriorityThread  extends  Thread { public  PriorityThread ( String  name)  { super ( name) ; } @Override public  void  run ( )  { for  ( int  i =  0 ;  i <  10 ;  i++ )  { System . out. println ( "当前线程名称:"  +  this . getName ( ) ) ; System . out. println ( "当前线程优先权:"  +  this . getPriority ( ) ) ; } } } } 
public  class  DemoThreadControl01  { public  static  void  main ( String [ ]  args)  throws  InterruptedException  { 
new  ControlThread ( "张三" ) . start ( ) ; new  ControlThread ( "李四" ) . start ( ) ; new  ControlThread ( "王五" ) . start ( ) ; } static  class  ControlThread  extends  Thread  { public  ControlThread ( String  name)  { super ( name) ; } 
@Override public  void  run ( )  { for  ( int  i =  0 ;  i <  10 ;  i++ )  { if  ( "张三" . equals ( this . getName ( ) ) )  { System . out. println ( "遇到张三,开始礼让" ) ; yield ( ) ; try  { Thread . sleep ( 10 ) ; }  catch  ( InterruptedException  e)  { throw  new  RuntimeException ( e) ; } System . out. println ( this . getName ( ) + "正在执行..." ) ; }  else  if  ( "李四" . equals ( this . getName ( ) ) )  { System . out. println ( "遇到李四,开始礼让" ) ; yield ( ) ; try  { Thread . sleep ( 10 ) ; }  catch  ( InterruptedException  e)  { throw  new  RuntimeException ( e) ; } System . out. println ( this . getName ( ) + "正在执行..." ) ; }  else  { System . out. println ( this . getName ( ) + "正在执行..." ) ; } } } } } 
public  class  DemoThreadControl02  { public  static  void  main ( String [ ]  args)  throws  InterruptedException  { 
StopThread  thread1 =  new  StopThread ( "邱六" ) ; StopThread  thread2 =  new  StopThread ( "王五" ) ; thread1. start ( ) ; thread2. start ( ) ; } static  class  DaemonThread  extends  Thread  { public  DaemonThread ( String  name)  { super ( name) ; } @Override public  void  run ( )  { for  ( int  i =  0 ;  i <  10 ;  i++ )  { System . out. println ( "当前线程:"  +  this . getName ( )  +  "是否为守护线程:"  +  this . isDaemon ( ) ) ; if  ( "张三" . equals ( this . getName ( ) ) )  { try  { Thread . sleep ( 1000 ) ; }  catch  ( InterruptedException  e)  { throw  new  RuntimeException ( e) ; } } System . out. println ( this . getName ( )  +  "正在执行..." ) ; } } } static  class  StopThread  extends  Thread { public  StopThread ( String  name)  { super ( name) ; } @Override public  void  run ( )  { for  ( int  i =  0 ;  i <  10 ;  i++ )  { try  { Thread . sleep ( 100 ) ; }  catch  ( InterruptedException  e)  { throw  new  RuntimeException ( e) ; } if  ( "邱六" . equals ( this . getName ( ) )  &&  i ==  3 ) { 
this . interrupt ( ) ; } System . out. println ( "当前线程:"  +  this . getName ( )  +  "当前i:"  +  i) ; } } } } 
import  java. util.  Random ; public  class  DemoEatWatermelonCommentVar  { public  static  int  allNum = 100 ; public  static  void  main ( String [ ]  args)  { 
AllWatermelon  allWatermelon =  new  AllWatermelon ( ) ; PeopleThread  thread1 =  new  PeopleThread ( "张三" ,  allWatermelon) ; PeopleThread  thread2 =  new  PeopleThread ( "李四" ,  allWatermelon) ; thread1. start ( ) ; thread2. start ( ) ; } static  class  AllWatermelon  { private  int  allNum =  100 ; public  void  eatOne ( )  { allNum -=  1 ; } public  int  getAllNum ( )  { return  allNum; } } static  class  PeopleThread  extends  Thread  { int  eatNum =  0 ;   Random  random; AllWatermelon  allWatermelon; public  PeopleThread ( String  name,  AllWatermelon  allWatermelon)  { super ( name) ; random =  new  Random ( ) ; this . allWatermelon =  allWatermelon; } @Override public  void  run ( )  { while  ( true )  { if  ( allWatermelon. getAllNum ( )  >  0 )  { try  { Thread . sleep ( 10 ) ; }  catch  ( InterruptedException  e)  { throw  new  RuntimeException ( e) ; } eatNum +=  1 ; allWatermelon. eatOne ( ) ; System . out. println ( "当前线程:"  +  this . getName ( )  +  "正在吃第"  +  eatNum +  "块西瓜,"  +  "当前剩余的总西瓜数:"  +  allWatermelon. getAllNum ( ) ) ; if  ( eatNum %  13  ==  0 )  { System . out. println ( "当前线程:"  +  this . getName ( )  +  "吃到一颗坏瓜... 吐了..." ) ; } } } } } 
} 
public  class  DemoRunnable  { public  static  void  main ( String [ ]  args)  { MyRunnableThread  myRunnableThread =  new  MyRunnableThread ( ) ; new  Thread ( myRunnableThread, "线程1" ) . start ( ) ; new  Thread ( myRunnableThread, "线程2" ) . start ( ) ; } static  class  MyRunnableThread  implements  Runnable { int  allNum =  100 ; @Override public  void  run ( )  { while  ( true ) { if  ( allNum <= 0 ) { break ; } else  { allNum -=  1 ; System . out. println ( "当前线程:" + Thread . currentThread ( ) . getName ( ) + "allNum:" + allNum) ; } } } } 
}