线程概念、线程与进程的区别、线程的运行状态参考《计算机操作系统》,本文仅关注于java的多线程开发方法。

1:java程序中进行多进程调度的两种方法:

使用runtime类,使用processBuilder类

java中实现一个线程的两种方法:

a)实现Runable接口实现它的run()方法

b)继承Thread类,覆盖它的run()方法。

这两种方法的区别是,如果你的类已经继承了其他的类,只能选择第一种,因为java只允许单继承。

package test;

 

import java.util.Date;

 

public class TestRunable implements Runnable{

public int time;

public String user;

public TestRunable(int time,String user){

this.time=time;

this.user=user;

}

public void run(){

while(true){

try{

System.out.println(user+"rest"+time+"ms"+new Date());

Thread.sleep(time);

}catch (Exception e) {

// TODO: handle exception

System.out.println(e);

}

}

}

public static void main(String args[]){

TestRunable t1=new TestRunable(1000,"me");

TestRunable t2=new TestRunable(5000,"you");

new Thread(t1).start();

new Thread(t2).start();

}

}

package test;

 

import java.util.Date;

 

public class TestThread extends Thread{

public int time;

public String user;

public TestThread(int time,String user){

this.time=time;

this.user=user;

}

public void run(){

while(true){

try{

System.out.println(user+"rest"+time+"ms"+new Date());

Thread.sleep(time);

}catch (Exception e) {

// TODO: handle exception

System.out.println(e);

}

}

}

public static void main(String args[]){

TestThread t1=new TestThread(1000,"me");

TestThread t2=new TestThread(5000,"you");

t1.start();

t2.start();

}

}