分别使用了Thread类 Runnable接口实现相同的计算过程:
下面这段代码是用Thread类实现:
/**
* @(#)MThread.java
*
*
* @author
* @version 1.00 2008/4/19
*/
public class MThread extends Thread {
private static int i=0;
private int num;
private int flag;
public MThread() {
new MThread(0);
}
public MThread(int flag){
this.num=++i;
this.flag=flag;
}
public void run(){
while(this.flag>=0){
System.out.println("The Thread "+this.num+":"+this.flag);
this.flag--;
try{
this.sleep((new java.util.Random()).nextInt(1000));
}catch(InterruptedException e){
System.out.println(e.toString());
}
}
}
public static void main(String args[]){
MThread mt0=new MThread(10);
MThread mt1=new MThread(20);
MThread mt2=new MThread(30);
mt0.start();
mt1.start();
mt2.start();
}
}
下面这段代码用Runnable接口实现:
/**
* @(#)cRunnable.java
*
*
* @author
* @version 1.00 2008/4/19
*/
public class cRunnable implements Runnable{
private static int i=0;
private int num;
private int flag;
public cRunnable() {
}
public cRunnable(int flag){
this.num=++i;
this.flag=flag;
}
public void run(){
while(this.flag>=0){
System.out.println("The Thread "+this.num+":"+this.flag);
this.flag--;
try{
new Thread(this).sleep((new java.util.Random()).nextInt(1000));
}catch(InterruptedException e){
System.out.println(e.toString());
}
}
}
public static void main(String args[]){
cRunnable cr0=new cRunnable(10);
cRunnable cr1=new cRunnable(10);
cRunnable cr2=new cRunnable(10);
new Thread(cr0).start();
new Thread(cr1).start();
new Thread(cr2).start();
}
}
呵呵是不是很无聊 这么简单的程序还拿出来炫耀!!
我果真很无聊啊!!
下面这段代码是用Thread类实现:
/**
* @(#)MThread.java
*
*
* @author
* @version 1.00 2008/4/19
*/
public class MThread extends Thread {
private static int i=0;
private int num;
private int flag;
public MThread() {
new MThread(0);
}
public MThread(int flag){
this.num=++i;
this.flag=flag;
}
public void run(){
while(this.flag>=0){
System.out.println("The Thread "+this.num+":"+this.flag);
this.flag--;
try{
this.sleep((new java.util.Random()).nextInt(1000));
}catch(InterruptedException e){
System.out.println(e.toString());
}
}
}
public static void main(String args[]){
MThread mt0=new MThread(10);
MThread mt1=new MThread(20);
MThread mt2=new MThread(30);
mt0.start();
mt1.start();
mt2.start();
}
}
下面这段代码用Runnable接口实现:
/**
* @(#)cRunnable.java
*
*
* @author
* @version 1.00 2008/4/19
*/
public class cRunnable implements Runnable{
private static int i=0;
private int num;
private int flag;
public cRunnable() {
}
public cRunnable(int flag){
this.num=++i;
this.flag=flag;
}
public void run(){
while(this.flag>=0){
System.out.println("The Thread "+this.num+":"+this.flag);
this.flag--;
try{
new Thread(this).sleep((new java.util.Random()).nextInt(1000));
}catch(InterruptedException e){
System.out.println(e.toString());
}
}
}
public static void main(String args[]){
cRunnable cr0=new cRunnable(10);
cRunnable cr1=new cRunnable(10);
cRunnable cr2=new cRunnable(10);
new Thread(cr0).start();
new Thread(cr1).start();
new Thread(cr2).start();
}
}
呵呵是不是很无聊 这么简单的程序还拿出来炫耀!!
我果真很无聊啊!!
没有评论:
发表评论