线程学习之Runable接口

class HelloThread implements Runnable {
	String message;
	HelloThread( String message ) {
		this.message = message;
	}
	public void run() {
		int sleeptime = ( int )( Math.random() * 3000 );
		try {
			Thread.sleep( sleeptime );
			System.out.println( message );
		}catch( InterruptedException e ) {
			System.out.print( message );
		}
	}
	
	public static void main(String[] args) {
		HelloThread ht1 = new HelloThread( "Good " );
		Thread t1 = new Thread( ht1 );
		
		HelloThread ht2 = new HelloThread( "morning");
		Thread t2 = new Thread( ht2 );
		
		HelloThread ht3 = new HelloThread( "to " );
		Thread t3 = new Thread( ht3 );
		
		t1.start();
		t2.start();
		t3.start();
		
		try {
			Thread.sleep( 1000 );
		}catch( InterruptedException e ) {}
		
		System.out.println( " You!" ); 
	}
}

 

你可能感兴趣的:(thread)