可以重载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为上述程序提供的运行时堆栈:
注: 重载的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++库](https://www.yiteyi.com/wp-content/uploads/geeks/geeks_Threadt1-300x218.png)
相关文章: 重载线程类start()方法的重写
本文由 比沙尔·库马尔·杜比 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END