`
uule
  • 浏览: 6312300 次
  • 性别: Icon_minigender_1
  • 来自: 一片神奇的土地
社区版块
存档分类
最新评论

Thread例子

 
阅读更多

看看对Thread到底懂多少,嘿嘿

 

 Example1:

public class Bground extends Thread{
    public static void main(String argv[])
    {
        Bground b = new Bground();
        b.run();
    }
    public void start()
    {
       for (int i = 0; i <10; i++){
           System.out.println("Value of i = " + i);
       }
    }
}

 结果:

编译无错,但不打印任何值。调用run不创建线程

 

Example2:

 

public class Bground extends Thread{
    public static void main(String argv[])
    {
        //Bground b = new Bground();
    	Thread b = new Thread(new MyRunnable());
        b.run();
    }
    
    public void start()
    {
       for (int i = 0; i <10; i++){
           System.out.println("Value of i = " + i);
       }
    }
}

	class MyRunnable implements Runnable{

		@Override
		public void run() {
			for (int i = 0; i <10; i++){
		           System.out.println("Value of i = " + i);
		       }
		}
		
	}

 结果:

Value of i = 0

Value of i = 1

Value of i = 2

Value of i = 3

Value of i = 4

Value of i = 5

Value of i = 6

Value of i = 7

Value of i = 8

 

Value of i = 9

 

不是说线程是调用start()方法执行的么,怎么调用run()方法也执行了呢??

查看API发现:

 

/**
     * Causes this thread to begin execution; the Java Virtual Machine 
     * calls the <code>run</code> method of this thread. 
     */
    public synchronized void start() {..}


/**
     * If this thread was constructed using a separate 
     * <code>Runnable</code> run object, then that 
     * <code>Runnable</code> object's <code>run</code> method is called; 
     * otherwise, this method does nothing and returns. 
     */
    public void run() {
	if (target != null) {
	    target.run();
	}
    }

 

调用线程类的run()方法时,如果该线程是实现的Runnable接口,那么调用run()时就会执行该Runnable,否则无反应

原来如此!

 

Example3:

 

public class Bground extends Thread{
    public static void main(String argv[])
    {
        Bground b = new Bground();    	
        b.start();
    }
    
    public void start()
    {
       for (int i = 0; i <10; i++){
           System.out.println("Value of i = " + i);
       }
    }
}

 

结果:

Value of i = 0

Value of i = 1

Value of i = 2

Value of i = 3

Value of i = 4

Value of i = 5

Value of i = 6

Value of i = 7

Value of i = 8

 

Value of i = 9

 

我可以重载start()方法么?
因为Thread类的start()方法并不是final方法,因此可以,但不推荐这样做。因为在start()方法里创建一个新的线程,进行特定的操作。你可以传递一个实现了Runnable接口的类到Thread,或者继承Thread类,重载run()方法。

 

此处启动线程时会调用线程的run()方法(此处为空),但本身的start()方法也被当作普通方法执行。

 

注意继承了Thread的线程类,里面的实现方法也是run()方法,跟Runnable接口的一样。

 

public class MyThread extends Thread {  
  
    public void run() {  
        for (int i = 0; i < 100; i++) {  
            ...
        }  
    }  
  
    public static void main(String[] args) {  
        new MyThread().start();  
    }  
}  

  

Example4:

public class Bground extends Thread {
	public static void main(String argv[]) {
		Bground b = new Bground();

		b.start();
		System.out.println(b.isAlive());
		//System.out.println(Thread.currentThread().getThreadGroup().getName());
	}

	public void start() {
		//System.out.println(Thread.currentThread().getThreadGroup().getName());
		for (int i = 0; i < 10; i++) {
			System.out.println("Value of i = " + i);
		}
	}

	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println("Run : Value of i = " + i);
		}
	}
}

 又有run()方法,又有start()方法,结果多少??

结果:

Value of i = 0

Value of i = 1

Value of i = 2

Value of i = 3

Value of i = 4

Value of i = 5

Value of i = 6

Value of i = 7

Value of i = 8

Value of i = 9

 

false

 

说明什么??

说明此时线程根本没有执行,像一个普通类一样仅调用了start()方法。

 

那么去掉start()方法呢?

结果:

true

Run : Value of i = 0

Run : Value of i = 1

Run : Value of i = 2

Run : Value of i = 3

Run : Value of i = 4

Run : Value of i = 5

Run : Value of i = 6

Run : Value of i = 7

Run : Value of i = 8

 

Run : Value of i = 9

 

此时线程才真正执行了!

 

Example5: 

 

public class Test extends Thread {
    private String sThreadName;
     
    Test() { }
 
    Test(String s) {
        sThreadName = s;
    }
 
    public String getThreadName() {
        return sThreadName;
    }
 
    public void go() {
        Test first = new Test("first");
        first.start();
        Test second = new Test("second");
        second.start();
    }
    
    public static void main(String argv[]) {
        Test h = new Test();
        h.go();
    }
 
 
    public void start() {
        for (int i = 0; i < 2; i++) {
            System.out.println(getThreadName() + i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
 

 

 结果你以为是:输出first0, second0, first1, second1.(1)

哈哈,错了!!!

结果为:

 first0

first1

second0

second1

 

如果要实现(1)的效果的话应该重载run,而不是start,此处仅相当于执行了start()方法。

 

 Example6:

public class Test {
    public static void main(String argv[]) {
        Pmcraven pm1 = new Pmcraven("one");
        pm1.run();
        Pmcraven pm2 = new Pmcraven("two");
        pm2.run();
    }
}
 
class Pmcraven extends Thread {
    private String sTname = "";
 
    Pmcraven(String s) {
        sTname = s;
    }
 
    public void run() {
        for (int i = 0; i < 2; i++) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
            }
            yield();
            System.out.println(sTname);
        }
    }
}

 

结果:

one

one

two

two

 

注意sleep(1000)方法依然起作用,结果是1秒显示一个。

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics