一、Thread类
Thread是Java中实现多线程的类,Thread中的run()方法实现了线程的启动。通过继承Thread类并覆写run()方法可实现一个线程。Thread类在java.lang包中实现,而这个包是默认载入的,因此不需要import。
ThreadDome01.java
程序ThreadDemo01.java中实现了两个线程,但在执行时没有同时执行,这是由于虽然在主程序中建立了两个线程,但是将两个线程顺序执行。若要并发执行多个线程,还需要启动操作系统对多线程的支持,应当采用Thread类中的start()方法。将主程序改成:
注意:,start()会自动去调用run()方法。不需要覆写Thread类中的start()方法。
二、Runable接口
由于Java是单继承关系,如果继承了Thread类就无法再继承其他的类,因此继承Thread的方法不常用,Java中还提供了一个实现多线程的方法,即Runable接口。
由于Runable接口中只定义了一个方法,即run(),之前提到多线程需要操作系统的支持,而run()方法是无法启动的,需要通过Thread类的start()启动操作系统对多线程的支持。在Thread类中提供了一个以Runabel类型参数的构造函数,因此另一个实现多线程的方法是先实现Runable接口,然后再通过Thread(Runable)构建一个线程,通过Thread中的start()方法间接启动操作系统对多线程的支持。如下例子如示:
ThreadDome02.java
Thread是Java中实现多线程的类,Thread中的run()方法实现了线程的启动。通过继承Thread类并覆写run()方法可实现一个线程。Thread类在java.lang包中实现,而这个包是默认载入的,因此不需要import。
ThreadDome01.java
- class Demo extends Thread
- {
- private string name ;
- Demo( String name)
- {
- this.name = name ;
- }
- public void run()
- {
- for( int i = 0; i < 10 ; i++ )
- System.out.println( this.name + “ running a thread……” ) ;
- }
- }
- // 主程序
- public class ThreadDome01
- {
- public static void main( String arg[] )
- {
- Demo d1 = new Demo(“Thread_1”) ;
- Demo d2 = new Demo(“Thread_2”) ;
- d1.run() ;
- d2.run() ;
- }
- }
class Demo extends Thread
{
private string name ;
Demo( String name)
{
this.name = name ;
}
public void run()
{
for( int i = 0; i < 10 ; i++ )
System.out.println( this.name + “ running a thread……” ) ;
}
}
// 主程序
public class ThreadDome01
{
public static void main( String arg[] )
{
Demo d1 = new Demo(“Thread_1”) ;
Demo d2 = new Demo(“Thread_2”) ;
d1.run() ;
d2.run() ;
}
}
程序ThreadDemo01.java中实现了两个线程,但在执行时没有同时执行,这是由于虽然在主程序中建立了两个线程,但是将两个线程顺序执行。若要并发执行多个线程,还需要启动操作系统对多线程的支持,应当采用Thread类中的start()方法。将主程序改成:
- public class ThreadDome01
- {
- public static void main( String arg[] )
- {
- Demo d1 = new Demo(“Thread_1”) ;
- Demo d2 = new Demo(“Thread_2”) ;
- d1.start() ;
- d2.start() ;
- }
- }
public class ThreadDome01
{
public static void main( String arg[] )
{
Demo d1 = new Demo(“Thread_1”) ;
Demo d2 = new Demo(“Thread_2”) ;
d1.start() ;
d2.start() ;
}
}
注意:,start()会自动去调用run()方法。不需要覆写Thread类中的start()方法。
二、Runable接口
由于Java是单继承关系,如果继承了Thread类就无法再继承其他的类,因此继承Thread的方法不常用,Java中还提供了一个实现多线程的方法,即Runable接口。
由于Runable接口中只定义了一个方法,即run(),之前提到多线程需要操作系统的支持,而run()方法是无法启动的,需要通过Thread类的start()启动操作系统对多线程的支持。在Thread类中提供了一个以Runabel类型参数的构造函数,因此另一个实现多线程的方法是先实现Runable接口,然后再通过Thread(Runable)构建一个线程,通过Thread中的start()方法间接启动操作系统对多线程的支持。如下例子如示:
ThreadDome02.java
- class Demo implements Runable
- {
- private string name ;
- Demo( String name)
- {
- this.name = name ;
- }
- public void run()
- {
- for( int i = 0; i < 10 ; i++ )
- System.out.println( this.name + “ running a thread……” ) ;
- }
- }
- // 主程序
- public class ThreadDome02
- {
- public static void main( String arg[] )
- {
- Demo d1 = new Demo(“Thread_1”) ;
- Demo d2 = new Demo(“Thread_2”) ;
- //启动多线程支持
- Thread t1 = new Thread(d1);
- Thread t2 = new Thread(d2);
- t1.start() ;
- t2.start() ;
- }
- }
class Demo implements Runable
{
private string name ;
Demo( String name)
{
this.name = name ;
}
public void run()
{
for( int i = 0; i < 10 ; i++ )
System.out.println( this.name + “ running a thread……” ) ;
}
}
// 主程序
public class ThreadDome02
{
public static void main( String arg[] )
{
Demo d1 = new Demo(“Thread_1”) ;
Demo d2 = new Demo(“Thread_2”) ;
//启动多线程支持
Thread t1 = new Thread(d1);
Thread t2 = new Thread(d2);
t1.start() ;
t2.start() ;
}
}