无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

多线程设计方法

Posted on 2009-07-16 22:32 Gavin.lee 阅读(355) 评论(0)  编辑  收藏 所属分类: 多线程

一:继承Thread,重载run方法
package com.Gavin.threaddesign;
/**
 * **********************************************
 * @description 继承Thread 类的多线程程序 设计方法 
 * 
 * 
@author Gavin.lee
 * @date Jul 16, 2009    2:25:41 PM
 * 
@version 1.0
 ***********************************************
 
*/

class Consumer extends Thread {
    
int nTime;
    String strConsumer;

    
public Consumer(int nTime, String strConsumer) {
        
this.nTime = nTime;
        
this.strConsumer = strConsumer;
    }


    
public void run() {
        
while (true{
            
try {
                System.out.println(
"Consumer name:" + strConsumer + "\n");
                Thread.sleep(nTime);
            }
 catch (Exception e) {
                e.printStackTrace();
            }

        }

    }


    
static public void main(String args[]) {
        Consumer aConsumer 
= new Consumer(1000"aConsumer");
        aConsumer.start();
        Consumer bConsumer 
= new Consumer(2000"bConsumer");
        bConsumer.start();
        Consumer cConsumer 
= new Consumer(3000"cConsumer ");
        cConsumer.start();
    }

}


二:实现Runnable接口,重写run方法
package com.Gavin.threaddesign;
/**
 * **********************************************
 * 多线程对象实现Runnable 接口并且在该类中定义用于启动线程的run 方法。
 * 这种定义方式的好处在于多线程应用对象可以继承其它对象而不是必须继承Thread 类,
 * 从而能够增加类定义的逻辑性
 * 
@author Gavin.lee
 * @date Jul 16, 2009    2:27:28 PM
 * 
@version 1.0
 ***********************************************
 
*/

public class Consumer2 implements Runnable {

    
int nTime;
    String strConsumer;

    
public Consumer2(int nTime, String strConsumer) {
        
this.nTime = nTime;
        
this.strConsumer = strConsumer;
    }


    
public void run() {

        
while (true{
            
try {
                System.out.println(
"Consumer name:" + strConsumer + "\n");
                Thread.sleep(nTime);
            }
 catch (Exception e) {
                e.printStackTrace();
            }

        }

    }


    
static public void main(String args[]) {
        Thread aConsumer 
= new Thread(new Consumer(1000"aConsumer"));
        aConsumer.start();

    }

}



2009年7月19日20:23:14 PS:
今天挑了一段尚学堂的一段线程的视频,感觉这个例子更容易理解:
实现Runnable接口
package com.Gavin.threaddesign;

public class TestThread {
    
public static void main(String args[]) {
        Thread1 t1 
= new Thread1();
        Thread t 
= new Thread(t1);
        t.start();    
//main方法出现执行分支
        
        
for(int i = 0; i < 50; i++{
            System.err.println(
"main thread:" + i);
        }

    }
        
}

class Thread1 implements Runnable{
    
public void run() {
        
for(int i = 0; i < 50; i++{
            System.err.println(
"Thread1 thread:" + i);
        }

    }
    
}

继承Thread类
package com.Gavin.threaddesign;

public class TestThread {
    
public static void main(String args[]) {
        Thread1 t1 
= new Thread1();
        t1.start();    
//main方法出现执行分支
        
        
for(int i = 0; i < 50; i++{
            System.err.println(
"main thread:" + i);
        }

    }
        
}

class Thread1 extends Thread{
    
public void run() {
        
for(int i = 0; i < 50; i++{
            System.err.println(
"Thread1 thread:" + i);
        }

    }
    
}

执行结果将是main线程与Thread1线程随机交替

只有注册用户登录后才能发表评论。


网站导航: