线程类run()方法的重载

可以重载run()方法。但是线程类start()方法不能调用任何参数方法。另一个重载方法必须像普通方法调用一样显式调用。

null

// Java Program to illustrate the behavior of
// run() method overloading
class Geeks extends Thread {
public void run()
{
System.out.println( "GeeksforGeeks" );
}
public void run( int i)
{
System.out.println( "Bishal" );
}
}
class Test {
public static void main(String[] args)
{
Geeks t = new Geeks();
t.start();
}
}


输出:

GeeksforGeeks

JVM为上述程序提供的运行时堆栈:

图片[1]-线程类run()方法的重载-yiteyi-C++库

注: 重载的run()方法将被Thread类忽略,除非我们自己调用它。Thread类需要一个不带arg的run(),它将在线程启动后在单独的调用堆栈中执行。使用run(inti),即使我们直接调用它,它也不会启动任何单独的调用堆栈。它将像任何其他方法一样位于同一个调用堆栈中(如果从run()方法调用)。

例子:

// Java Program to illustrate the execution of
// program using main thread
class Geeks extends Thread {
public void run()
{
System.out.println( "GeeksforGeeks" );
}
public void run( int i)
{
System.out.println( "Bishal" );
}
}
class Test extends Geeks {
public static void main(String[] args)
{
Geeks t = new Geeks();
t.run( 1 );
}
}


输出:

Bishal
    JVM为上述程序提供的运行时堆栈: 图片[2]-线程类run()方法的重载-yiteyi-C++库

相关文章: 重载线程类start()方法的重写

本文由 比沙尔·库马尔·杜比 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享