看看对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秒显示一个。
相关推荐
商业编程-源码-Csharp实例82 Thread例子3.zip
商业编程-源码-Csharp实例81 Thread例子2.zip
商业编程-源码-Csharp实例78 Thread例子1.zip
一个线程经典例子 Thread delegate void SetProgressBarValueDele(); void SetProgressBarValue() { if (this.progressBar1.InvokeRequired) { this.Invoke(new SetProgressBarValueDele...
Thread例子dome
android developer->training->sending operations to multiple threads 的例子。demo的例子叫threadSample.zip。这个例子请结合官网文档看,对android多线程处理给出了一个解决方案,写的相当好。
CH579M-RT-THREAD点灯例子
C#例子代码 A0303_ThreadSafeC#例子代码 A0303_ThreadSafeC#例子代码 A0303_ThreadSafeC#例子代码 A0303_ThreadSafeC#例子代码 A0303_ThreadSafeC#例子代码 A0303_ThreadSafeC#例子代码 A0303_ThreadSafeC#例子代码 ...
VB多线程各种例子Thread Factory 4(中文帮助文档)
C#例子代码 A0300_ThreadC#例子代码 A0300_ThreadC#例子代码 A0300_ThreadC#例子代码 A0300_ThreadC#例子代码 A0300_ThreadC#例子代码 A0300_ThreadC#例子代码 A0300_ThreadC#例子代码 A0300_ThreadC#例子代码 A0300...
官方推荐的多线程例子,在train中推荐的,但是因为某些原因,下载不到,另外的一个资源要的积分太多,特贡献
C#例子代码 A0301_ThreadAbortC#例子代码 A0301_ThreadAbortC#例子代码 A0301_ThreadAbortC#例子代码 A0301_ThreadAbortC#例子代码 A0301_ThreadAbortC#例子代码 A0301_ThreadAbortC#例子代码 A0301_ThreadAbortC#...
THREADX 2002版本VS2013带所有测试例子 PC版模拟版本 非常好用。
复制代码 代码如下:# encoding: UTF-8import threadimport time# 一个用于在线程中执行的函数def func(): for i in range(5): print ‘func’ time.sleep(1) # 结束当前线程 # 这个方法与thread.exit_thread()...
vc 的多线程例子
java多线程thread实例,经过测试的,可以直接运行
入门线程,和高级线程。有助于大家的学习,大家可以看看。
首先看看boost::thread的构造函数吧,boost::thread有两个构造函数: (1)thread():构造一个表示...该构造函数传入的是函数对象而并非是函数指针,这样一个具有一般函数特性的类也能作为参数传入,在下面有例子。
vs2013 上根据培训内容抄的Win32 API实现的windows环境下的文件读写/内存分配/线程同步等小实例。适合初学者学习和长时间很少接触Win32 API的开发者快速了解
本人写的比较简单的多线程程序,可设置线程优先级,方便入门学习。