java中实现多线程有两种方法:继承Thread类和实现Runable接口。(实际上Thread类实现了Runable接口)
当用户线程类仅仅继承Thread类,则选用第一种方法,然而当要继承多个类,则实现Runable接口。两种方法
的实现如下:
一、继承Thread类
//
定义自己的线程类
class Mythread extends Thread
{
// 重写Thread类中的Run函数,该类所有的功能均在Run函数中运行
public void run()
{
// ............
}
}
// 使用该线程类
class ThreadDemo
{
public static void main(String[] args)
{
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
// 调用start函数启动该线程,该函数的功能是启动run方法
thread1.start();
thread2.start();
}
}
class Mythread extends Thread
{
// 重写Thread类中的Run函数,该类所有的功能均在Run函数中运行
public void run()
{
// ............
}
}
// 使用该线程类
class ThreadDemo
{
public static void main(String[] args)
{
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
// 调用start函数启动该线程,该函数的功能是启动run方法
thread1.start();
thread2.start();
}
}
二、实现Runable接口
//
定义自己的线程类
class MyThread implements Runnable
{
// 实现Runable接口中的Run函数,该类所有的功能均在Run函数中运行
public void run()
{
// ............
}
}
// 使用该线程类
class ThreadDemo
{
public static void main(String[] args)
{
Thread thread1 = new Thread(new MyThread());
Thread thread2 = new Thread(new MyThread());
// 调用start函数启动该线程,该函数的功能是启动run方法
thread1.start();
thread2.start();
}
}
class MyThread implements Runnable
{
// 实现Runable接口中的Run函数,该类所有的功能均在Run函数中运行
public void run()
{
// ............
}
}
// 使用该线程类
class ThreadDemo
{
public static void main(String[] args)
{
Thread thread1 = new Thread(new MyThread());
Thread thread2 = new Thread(new MyThread());
// 调用start函数启动该线程,该函数的功能是启动run方法
thread1.start();
thread2.start();
}
}
三、使用内类简化上述方法
public
void
oneMethod()
{
Thread thread = new Thread( new Runnable() // 这里的Runnable也可以换成Thread
{
public void run()
{
// 在这里添加运行代码....
// while(your boolVar)
// {
// .........
// }
}
});
thread.start();
}
{
Thread thread = new Thread( new Runnable() // 这里的Runnable也可以换成Thread
{
public void run()
{
// 在这里添加运行代码....
// while(your boolVar)
// {
// .........
// }
}
});
thread.start();
}
下面的程序中,Test中的synchronized public void printNumber(int id)方法打印从1到100的数,其中id为调用该方法的线程的id,以表明是那个线程在调用该方法。方法中的代码实现了两个线程轮流打印10个数。具体代码如下:
public
class
MyThread
implements
Runnable
{
private Test instance;
private int id;
MyThread( int id,Test instance)
{
this .id = id;
this .instance = instance;
}
public void run()
{
instance.printNumber(id);
}
}
{
private Test instance;
private int id;
MyThread( int id,Test instance)
{
this .id = id;
this .instance = instance;
}
public void run()
{
instance.printNumber(id);
}
}
public
class
Test
{
synchronized public void printNumber( int id)
{
for ( int i = 1 ;i <= 100 ;i ++ )
{
System.out.println(i + " ---- " + id);
if (i % 10 == 0 )
{
this .notify();
if (i != 100 )
{
try
{
this .wait();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
}
}
public static void main(String[] args)
{
Test test = new Test();
Thread thread1 = new Thread( new MyThread( 1 ,test));
Thread thread2 = new Thread( new MyThread( 2 ,test));
thread1.start();
thread2.start();
}
}
{
synchronized public void printNumber( int id)
{
for ( int i = 1 ;i <= 100 ;i ++ )
{
System.out.println(i + " ---- " + id);
if (i % 10 == 0 )
{
this .notify();
if (i != 100 )
{
try
{
this .wait();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
}
}
public static void main(String[] args)
{
Test test = new Test();
Thread thread1 = new Thread( new MyThread( 1 ,test));
Thread thread2 = new Thread( new MyThread( 2 ,test));
thread1.start();
thread2.start();
}
}