黑马程序员技术交流社区
标题:
菜鸟求问题解答..
[打印本页]
作者:
芦青
时间:
2013-1-27 13:49
标题:
菜鸟求问题解答..
本帖最后由 张向辉 于 2013-1-29 11:44 编辑
class xc implements Runnable
{
public void run()
{
//复写Runnable的Run方法..
}
}
public class lei
{
public static void main(String[] args)
{
{
xc a=new xc();
Thread t=new Thread(a);
t.start();
}
}
为什么Runnable接口的子类对象当成实际参数传递给Thread构造函数的时候..当Thread调用Start方法开启线程时,就可以调用到
Runnable接口的子类对象中的run方法了??求精辟点,易理解的语言...
作者:
刘军亭
时间:
2013-1-27 14:19
在Thread类的构造函数时候可以接受一个对象,但是为了安全规范这个对象必须实现Runnable接口。Thread类还有判断创建Thread对象时候传进来的对象是否为null,如果不为null就运行传进来的Runnable对象的run()方法
作者:
vmvm555
时间:
2013-1-27 16:35
托你的福,我第一次跑去看源码了,下面附上源代码
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
if (name == null) {
throw new NullPointerException("name cannot be null");
}
Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */
/* If there is a security manager, ask the security manager
what to do. */
if (security != null) {
g = security.getThreadGroup();
}
/* If the security doesn't have a strong opinion of the matter
use the parent thread group. */
if (g == null) {
g = parent.getThreadGroup();
}
}
/* checkAccess regardless of whether or not threadgroup is
explicitly passed in. */
g.checkAccess();
/*
* Do we have the required permissions?
*/
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
}
g.addUnstarted();
this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
this.name = name.toCharArray();
if (security == null || isCCLOverridden(parent.getClass()))
this.contextClassLoader = parent.getContextClassLoader();
else
this.contextClassLoader = parent.contextClassLoader;
this.inheritedAccessControlContext = AccessController.getContext();
this.target = target;//看懂这句话就行了
setPriority(priority);
if (parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize;
/* Set thread ID */
tid = nextThreadID();
}
复制代码
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()方法就行了,因为你没抛出异常,所以catch语句和finally语句也无需看,这样就简单了
作者:
杨玲
时间:
2013-1-27 17:05
本帖最后由 杨玲 于 2013-1-27 17:18 编辑
楼上源码都帖出来了,呵呵,不过简单总结一下就是在Thread类中定义了一个Runnable接口的引用,并实现了Runnable接口,覆写了它的run方法,在该方法中会先判断这个引用是否为空,如果是以继承Thread形式存在的话,这个run方法就被覆写了,它会调用被子类覆写的run方法,如果是实现Runnable接口的,并把其作为参数传递时,那么这时这个Runnable的引用不为空,在run方法中就会调用这个引用中的run方法.
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2