Java之多线程易范错误


  1. public class mainTestImpl {   
  2.   
  3.     /*  
  4.      * 任何线程一般具有五种状态,即创建、就绪、运行、阻塞、终止 线程 调用stop()方法时或run()方法执行  
  5.      * 结束后,线程即处于死亡状态。 处于死亡状态的线程不具有继续运行的能力。  
  6.      */  
  7.   
  8.     public static void main(String[] args) {  
  9.          testThread t = new testThread();  
  10.          t.run(); // start是启动一个线程由程序来调用run方法  
  11.         // run 是运行一个方法,等结束以后再执行后面的代码,不能达到多线程目的  
  12.          System.out.println(Thread.currentThread().getName() + " 线程运行结束!");  
  13.   
  14.     }  
  15. }  
  16.   
  17.   
  18. // 通过类继承必须有run方法,因为start启动线程后由程序调用run方法  
  19. // Thread同样实现了Runnable接口  
  20. class testThread extends Thread {  
  21.     public void run() {  
  22.         for (int i = 0; i < 5; i++) {  
  23.             try {  
  24.                 Thread.currentThread().sleep(500);  
  25.             } catch (InterruptedException e) {  
  26.                 e.printStackTrace();  
  27.             }  
  28.             System.out.println("runnable=" + Thread.currentThread().getName() + " " + i);  
  29.         }  
  30.         //new threadImp().run();  
  31.     }  
  32. }  
public class mainTestImpl { 

	/*
	 * 任何线程一般具有五种状态,即创建、就绪、运行、阻塞、终止 线程 调用stop()方法时或run()方法执行
	 * 结束后,线程即处于死亡状态。 处于死亡状态的线程不具有继续运行的能力。
	 */

	public static void main(String[] args) {
		 testThread t = new testThread();
		 t.run(); // start是启动一个线程由程序来调用run方法
		// run 是运行一个方法,等结束以后再执行后面的代码,不能达到多线程目的
         System.out.println(Thread.currentThread().getName() + " 线程运行结束!");

	}
}


// 通过类继承必须有run方法,因为start启动线程后由程序调用run方法
// Thread同样实现了Runnable接口
class testThread extends Thread {
	public void run() {
		for (int i = 0; i < 5; i++) {
			try {
				Thread.currentThread().sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("runnable=" + Thread.currentThread().getName() + " " + i);
		}
		//new threadImp().run();
	}
}
运行后的结果如下:

  1. runnable=main 0  
  2. runnable=main 1  
  3. runnable=main 2  
  4. runnable=main 3  
  5. runnable=main 4  
  6. main 线程运行结束!  
runnable=main 0
runnable=main 1
runnable=main 2
runnable=main 3
runnable=main 4
main 线程运行结束!
可以看到都是main线程在运行,并不是期望的多线程。原因就在于调用了run()方法,将如上对应代码段改为如下后运行:

  1. t.start(); // start是启动一个线程由程序来调用run方法  
  2. // run 是运行一个方法,等结束以后再执行后面的代码,不能达到多线程目的  
t.start(); // start是启动一个线程由程序来调用run方法
// run 是运行一个方法,等结束以后再执行后面的代码,不能达到多线程目的
  1. main 线程运行结束!  
  2. runnable=Thread-0 0  
  3. runnable=Thread-0 1  
  4. runnable=Thread-0 2  
  5. runnable=Thread-0 3  
  6. runnable=Thread-0 4  
main 线程运行结束!
runnable=Thread-0 0
runnable=Thread-0 1
runnable=Thread-0 2
runnable=Thread-0 3
runnable=Thread-0 4

修改testThread线程类并且增加一个实现Runnable接口的线程类,代码如下:

  1. // 通过类继承必须有run方法,因为start启动线程后由程序调用run方法  
  2. // Thread同样实现了Runnable接口  
  3. class testThread extends Thread {  
  4.     public void run() {  
  5.         new threadImp().run(); // 启动线程  
  6.     }  
  7. }  
  8.   
  9. class threadImp implements Runnable {  
  10.     // 线程的几个状态,判断属性,计数次数,同步方法  
  11.     public void run() {  
  12.             for (int i = 0; i < 50; i++) {  
  13.                 System.out.println("runnable=" + Thread.currentThread().getName() + " " + i);  
  14.             }  
  15.     }  
  16. }  
// 通过类继承必须有run方法,因为start启动线程后由程序调用run方法
// Thread同样实现了Runnable接口
class testThread extends Thread {
	public void run() {
		new threadImp().run(); // 启动线程
	}
}

class threadImp implements Runnable {
	// 线程的几个状态,判断属性,计数次数,同步方法
	public void run() {
			for (int i = 0; i < 50; i++) {
				System.out.println("runnable=" + Thread.currentThread().getName() + " " + i);
			}
	}
}
通过mainTestImpl类中的testThread类的run方法来启动另外一个线程, 发现运行的结果还是单线程,不知道为什么。。。。。。。

















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值