Java多线程

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多线程,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。