Java多线程

时间:2014-04-03 15:07:59   收藏:0   阅读:485

Java多线程的线程类需继承于Thread类,并且需要重写run函数。

sleep休眠时使用Thread.sleep( 200 )这种形式,并且要放在try-catch语句块中。

 下面给出简单demo:

class MyThread extends Thread
{
public MyThread( String strName0 )
{
super( strName0 );
}

public void run()
{
for( int i=0; i<10; ++i )
{
System.out.println( "Thread name is: " + this.getName() );
try
{
Thread.sleep(300);
}
catch( InterruptedException e )
{
System.out.println( e.getMessage() );
}

}
}
}

 

public class TestMultiThread
{

public static void main(String[] args)
{
MyThread thread1 = new MyThread( "ThreadA" );
MyThread thread2 = new MyThread( "ThreadB" );
//MyThread thread3 = new MyThread( "ThreadC" );

thread1.start();
thread2.start();
//thread3.start();

}

}

Java多线程,布布扣,bubuko.com

原文:http://www.cnblogs.com/MiniHouse/p/3642630.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!