avatar
java的接口runnable# Java - 爪哇娇娃
e*m
1
一直搞得不是很清楚,不知道前人问过没有。 java内实现多线程的时候可以通过实现
借口runnable, 既然runnable是接口,没有任何的内部机制,怎么实现的多线程? 难
道是jvm运行的时候,碰到实现runnable的对象就当成多线程的程序来处理,实际上最
终的多线程的实现是在jvm中实现的,而不是在设计对象的时候实现的。
如果用c++来实现多线程,因为语言本身不支持,就必须自己设计的对象亲自处理多线
程运行的各种情况了吧? 有没有现成的c++类库已经实现了多线程了?
欢迎各位大师的指导。
avatar
c*t
2
Well, in Java you need to do
Runnable myRunnable = new Runable () { ... };
Thread myThread = new Theaqd (myRunnable);
myThread.start ();
Simply creating Runnable object (myRunnable) does not create a new
thread.
Synchronizations etc are a whole new set issues.

【在 e****m 的大作中提到】
: 一直搞得不是很清楚,不知道前人问过没有。 java内实现多线程的时候可以通过实现
: 借口runnable, 既然runnable是接口,没有任何的内部机制,怎么实现的多线程? 难
: 道是jvm运行的时候,碰到实现runnable的对象就当成多线程的程序来处理,实际上最
: 终的多线程的实现是在jvm中实现的,而不是在设计对象的时候实现的。
: 如果用c++来实现多线程,因为语言本身不支持,就必须自己设计的对象亲自处理多线
: 程运行的各种情况了吧? 有没有现成的c++类库已经实现了多线程了?
: 欢迎各位大师的指导。

avatar
e*m
3
ok, now it is very clear. Finally, the object that implements the interface
has to be used by a Thread Object.
avatar
c*t
4
You got it wrong. Runnable is merely a interface. To start a thread,
you need to create a new Thread instance. So
new Runnable ()
{
public void run () { ... }
}.run ();
doesn't create a new thread.
As for C++, you can create your own thread class using threading api's,
such as pthread on unix. It is fairly trivial to do so.

【在 e****m 的大作中提到】
: 一直搞得不是很清楚,不知道前人问过没有。 java内实现多线程的时候可以通过实现
: 借口runnable, 既然runnable是接口,没有任何的内部机制,怎么实现的多线程? 难
: 道是jvm运行的时候,碰到实现runnable的对象就当成多线程的程序来处理,实际上最
: 终的多线程的实现是在jvm中实现的,而不是在设计对象的时候实现的。
: 如果用c++来实现多线程,因为语言本身不支持,就必须自己设计的对象亲自处理多线
: 程运行的各种情况了吧? 有没有现成的c++类库已经实现了多线程了?
: 欢迎各位大师的指导。

avatar
e*m
5
发现java最牛的就是你了,对你得kobe印象太深了。
是不是jdk 里边自带的所有借口都有内嵌的支持。
但是如果我们自己定义一个接口的话,好像只是为了设计的原因。
C++中的多线程大师熟悉吗?
avatar
g*g
6
Runnable 确实没有特殊的,但Thread class应该不是纯
java code.
avatar
g*g
7

^^^^^^^
make it start() here, you create an anonymous class and
start a new thread, since no start() method is implemented in the
entire hierarchy, I believe there's some magic in JVM. and I'd
call that embeded support.

【在 c*****t 的大作中提到】
: You got it wrong. Runnable is merely a interface. To start a thread,
: you need to create a new Thread instance. So
: new Runnable ()
: {
: public void run () { ... }
: }.run ();
: doesn't create a new thread.
: As for C++, you can create your own thread class using threading api's,
: such as pthread on unix. It is fairly trivial to do so.

avatar
e*m
8
俩字-大师
avatar
w*r
9
erh???没明白,这不会出错???你连Thread都没有怎么call start()?

【在 g*****g 的大作中提到】
:
: ^^^^^^^
: make it start() here, you create an anonymous class and
: start a new thread, since no start() method is implemented in the
: entire hierarchy, I believe there's some magic in JVM. and I'd
: call that embeded support.

avatar
L*r
10
Thread model 是JVM的一部分. 所有的JVM都是native code 实现的.
avatar
g*g
11
ft, 太久不写多线程程序,确实是我弄错了。
//shy

【在 c*****t 的大作中提到】
: You got it wrong. Runnable is merely a interface. To start a thread,
: you need to create a new Thread instance. So
: new Runnable ()
: {
: public void run () { ... }
: }.run ();
: doesn't create a new thread.
: As for C++, you can create your own thread class using threading api's,
: such as pthread on unix. It is fairly trivial to do so.

avatar
L*r
12
惭愧. 我做了很长时间的java后, 才读了JVM Specification.
我们只是习惯用语不同.
对我而言,
你看到的JAVA code是纯java实现. 纯java实现的基本是binary compatible.
Java不纯的实现在JVM中.
JNI的一个基本要求就是要保证Java code pure java and binary compatible.
发信人: wyr (遗忘小资), 信区: Java
标 题: Re: java的接口runnable
发信站: BBS 未名空间站 (Fri Oct 6 12:46:18 2006), 转信
嗯,这是众所周知的事情呀~
JVM不就是一计算机嘛
avatar
w*r
13
Thread class 当然不会是纯java 的,对就不同的OS, jvm需要把Thread map 到不同的
OS上面去,在java source code里面就可以看到在Thread class 里面有如下的native
function
public static native Thread currentThread();
public static native void yield();
public static native void sleep(long millis) throws InterruptedException;
private native void start0();
private native boolean isInterrupted(boolean ClearInterrupted);
public final native boolean isAlive();
public static native boolean holdsLock(Object obj);
pr
avatar
L*r
14
你看来不了解JAVA的强大.
JAVA的thread model是在JVM中实现的.
native 就和static 等keyword一样, native只是标明JVM link method to a native
library. 并不是什么native code.
JAVA code 是binary compatible.
即使OS不支持多线程, JVM也可以实现多线程支持.
avatar
L*r
15
Thread的具体实现是Operating System(OS)的功能. OS有native thread API支持
thread programming.
在C++中, 你可以直接用os thread api做programming. 这要求你对thread用比较好的
理解.
C++也同时有一些library, 封装os thread API 成class, 例如CWinThread class 在
windows 下, 用这些class会让thread programming容易很多.
Java的Thread 是一个pure object concept, 程序员根本不需要了解具体的thread 实
现. Thread class 或者 runnable interface 和任何其他的class , interface没有太
大区别.
程序员只需要考虑business logic的实现. Java 让thread 变的很容易.
对于thread 的具体支持取决于JVM对thread class的native image.
goodbug (好虫) 说java的多线程支持是内嵌的
avatar
w*r
16
erh???没明白,这不会出错???你连Thread都没有怎么call start()?

【在 g*****g 的大作中提到】
: ft, 太久不写多线程程序,确实是我弄错了。
: //shy

avatar
L*r
17
runnable interface 不是什么hook, 只是一个pluggable pattern对于thread class,
JVM知道一个class 是thread是从你的thread instance.
Thread class compile后是pure java bytecode. JVM会对bytecode作native image.
Java thread programming很容易, 如果要考虑multi-threads synchronization 和
communication 才需要更深的了解.
闻道有先后,术业有专攻,如是而已
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。