黑马程序员技术交流社区
标题:
start方法的复写
[打印本页]
作者:
刘胜寒
时间:
2013-4-26 21:29
标题:
start方法的复写
本帖最后由 刘胜寒 于 2013-4-26 21:40 编辑
public class Main extends Thread
{
private int a;
public void start()
{
super.start();
}
public void run(){
System.out.println("线程"+currentThread().getName()+"启动");
for(a=0;a<40;a++){
System.out.println(currentThread().getName()+":"+a);
}
}
public static void main(String[] args) {
Main m1 = new Main();
m1.start();
Main m2 = new Main();
m2.start();
for(int x=0;x<40;x++)
{
System.out.println("main线程:"+x);
}
}
}
复制代码
这个块代码中复写了start方法,是去调用父类的start方法
下面是运行结果
线程Thread-0启动
main线程:0
main线程:1
Thread-0:0
main线程:2
线程Thread-1启动
main线程:3
Thread-0:1
main线程:4
Thread-1:0
main线程:5
main线程:6
main线程:7
main线程:8
main线程:9
main线程:10
main线程:11
main线程:12
Thread-0:2
Thread-0:3
main线程:13
main线程:14
main线程:15
Thread-1:1
main线程:16
Thread-0:4
main线程:17
main线程:18
main线程:19
main线程:20
main线程:21
main线程:22
main线程:23
main线程:24
main线程:25
main线程:26
main线程:27
main线程:28
main线程:29
main线程:30
main线程:31
main线程:32
main线程:33
main线程:34
main线程:35
main线程:36
main线程:37
main线程:38
main线程:39
Thread-1:2
Thread-0:5
Thread-0:6
Thread-0:7
Thread-0:8
Thread-0:9
Thread-0:10
Thread-0:11
Thread-0:12
Thread-0:13
Thread-0:14
Thread-0:15
Thread-0:16
Thread-0:17
Thread-0:18
Thread-0:19
Thread-1:3
Thread-0:20
Thread-1:4
Thread-1:5
Thread-0:21
Thread-1:6
Thread-0:22
Thread-1:7
Thread-1:8
Thread-0:23
Thread-1:9
Thread-0:24
Thread-1:10
Thread-0:25
Thread-0:26
Thread-1:11
Thread-0:27
Thread-1:12
Thread-0:28
Thread-0:29
Thread-0:30
Thread-1:13
Thread-0:31
Thread-0:32
Thread-0:33
Thread-0:34
Thread-0:35
Thread-1:14
Thread-0:36
Thread-1:15
Thread-0:37
Thread-0:38
Thread-1:16
Thread-0:39
Thread-1:17
Thread-1:18
Thread-1:19
Thread-1:20
Thread-1:21
Thread-1:22
Thread-1:23
Thread-1:24
Thread-1:25
Thread-1:26
Thread-1:27
Thread-1:28
Thread-1:29
Thread-1:30
Thread-1:31
Thread-1:32
Thread-1:33
Thread-1:34
Thread-1:35
Thread-1:36
Thread-1:37
Thread-1:38
Thread-1:39
这一段代码
public class Main extends Thread
{
private int a;
public void run(){
System.out.println("线程"+currentThread().getName()+"启动");
for(a=0;a<40;a++){
System.out.println(currentThread().getName()+":"+a);
}
}
public static void main(String[] args) {
Main m1 = new Main();
m1.start();
Main m2 = new Main();
m2.start();
for(int x=0;x<40;x++)
{
System.out.println("main线程:"+x);
}
}
}
复制代码
运行结果:
main线程:0
线程Thread-0启动
Thread-0:0
main线程:1
Thread-0:1
Thread-0:2
线程Thread-1启动
Thread-0:3
main线程:2
Thread-0:4
Thread-1:0
Thread-0:5
main线程:3
Thread-0:6
Thread-1:1
Thread-0:7
main线程:4
Thread-0:8
Thread-1:2
Thread-1:3
Thread-1:4
Thread-0:9
Thread-0:10
Thread-0:11
Thread-0:12
Thread-0:13
Thread-0:14
Thread-0:15
Thread-0:16
Thread-0:17
Thread-0:18
Thread-0:19
Thread-0:20
Thread-0:21
Thread-0:22
Thread-0:23
Thread-0:24
Thread-0:25
Thread-0:26
Thread-0:27
main线程:5
main线程:6
main线程:7
main线程:8
main线程:9
main线程:10
main线程:11
main线程:12
main线程:13
main线程:14
main线程:15
main线程:16
main线程:17
main线程:18
main线程:19
main线程:20
main线程:21
main线程:22
main线程:23
main线程:24
main线程:25
main线程:26
Thread-0:28
Thread-0:29
Thread-0:30
Thread-0:31
Thread-0:32
Thread-0:33
Thread-0:34
Thread-0:35
Thread-0:36
Thread-0:37
Thread-0:38
Thread-0:39
Thread-1:5
Thread-1:6
Thread-1:7
Thread-1:8
Thread-1:9
Thread-1:10
Thread-1:11
Thread-1:12
Thread-1:13
Thread-1:14
Thread-1:15
Thread-1:16
Thread-1:17
Thread-1:18
Thread-1:19
Thread-1:20
Thread-1:21
Thread-1:22
Thread-1:23
Thread-1:24
Thread-1:25
Thread-1:26
main线程:27
main线程:28
main线程:29
main线程:30
main线程:31
main线程:32
main线程:33
Thread-1:27
main线程:34
main线程:35
main线程:36
main线程:37
main线程:38
main线程:39
Thread-1:28
Thread-1:29
Thread-1:30
Thread-1:31
Thread-1:32
Thread-1:33
Thread-1:34
Thread-1:35
Thread-1:36
Thread-1:37
Thread-1:38
Thread-1:39
这两个结果没什么区别啊
谁能解释下Thread子类复写strat方法和Thread的start方法有什么区别吗
start()源码 我是没看懂
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
private native void start0();
/**
* 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.
* <p>
* Subclasses of <code>Thread</code> should override this method.
*
* @see #start()
* @see #stop()
* @see #Thread(ThreadGroup, Runnable, String)
*/
@Override
public void run() {
if (target != null) {
target.run();
}
}
复制代码
原帖链接
多线程问题之重写start方法
http://bbs.itheima.com/thread-47650-1-1.html
作者:
袁梦希
时间:
2013-4-26 21:32
本帖最后由 袁梦希 于 2013-4-26 21:34 编辑
{:soso_e173:}整真么长 我得慢慢看
老刘啊 其实我觉得 他们用的都是Thread类中的start()方法。只是开启线程的方式不同,
作者:
java冬冬
时间:
2013-4-26 21:33
本帖最后由 java冬冬 于 2013-4-26 21:47 编辑
慢慢看{:soso_e152:}
额额额,好吧{:soso_e127:}
作者:
殇_心。
时间:
2013-4-26 21:35
寒妹。你把我们坑了。。。
作者:
刘胜寒
时间:
2013-4-26 21:40
袁梦希 发表于 2013-4-26 21:32
整真么长 我得慢慢看
是我脑子不灵光了....
在不不写的情况下
子类调用的也是父类的....
复写只是显示的调用父类的start方法
去他妹的...
被贴子给误导了...
作者:
陈圳
时间:
2013-4-26 21:45
我晕死,你那不叫复写!套了层外衣,调的还是父类的方法.
你那个源码不全.我们在run方法内写多线程执行的代码,用start启动线程,我记得老师说过的,线程调的是CPU的资源,java没有那个能力的.他在start方法内,封装的是调用本地API方法去开启多线程的代码.用native 声明的就说明是本平台的方法.
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2