package thread ;
public class Test01 extends Thread {
int count = 0 ;
@Override
public void run ( ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + "线程运行的代码" ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
count++ ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "这是逻辑代码" + count) ;
}
}
}
class T {
public static void main ( String [ ] args) {
Test01 t = new Test01 ( ) ;
Test01 t1 = new Test01 ( ) ;
Thread tt = new Thread ( t) ;
Thread tt1 = new Thread ( t1) ;
tt. start ( ) ;
tt. setName ( "儿子" ) ;
tt1. setName ( "儿子1" ) ;
System . out. println ( "===========================" ) ;
tt1. start ( ) ;
System . out. println ( "=========================" ) ;
System . out. println ( tt. getName ( ) ) ;
System . out. println ( tt1. getName ( ) ) ;
System . out. println ( tt. getPriority ( ) ) ;
System . out. println ( tt1. getPriority ( ) ) ;
tt. setPriority ( 1 ) ;
tt1. setPriority ( 10 ) ;
System . out. println ( tt. getPriority ( ) ) ;
System . out. println ( tt1. getPriority ( ) ) ;
}
}
package thread ;
public class Demo01 extends Thread {
int count = 0 ;
@Override
public void run ( ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + "线程运行的代码" ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
try {
Thread . sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
if ( i % 2 == 0 ) {
Thread . yield ( ) ;
}
count++ ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "这是逻辑代码" + count) ;
}
}
}
class B {
public static void main ( String [ ] args) throws InterruptedException {
Demo01 demo01 = new Demo01 ( ) ;
Demo01 demo02 = new Demo01 ( ) ;
Thread thread1 = new Thread ( demo01) ;
Thread thread2 = new Thread ( demo02) ;
System . out. println ( "111111111111111111111111" ) ;
thread1. start ( ) ;
thread2. start ( ) ;
System . out. println ( thread2. isAlive ( ) ) ;
System . out. println ( "22222222222222222222222" ) ;
System . out. println ( "333333333333333333333" ) ;
thread1. join ( ) ;
System . out. println ( "4444444444444444444444" ) ;
}
}