Cás*_*rdo 5 java methods multithreading
这个问题令人困惑,但这就是我想要做的:
public class Main
{
MyClass instance = new MyClass();
Thread secondThread = new Thread(instance);
public static void main()
{
secondThread.start();
//here i want to call foo(), but be processed by secondThread thread(not the Main thread)
}
}
public class MyClass implements Runnable
{
@Override
public void run()
{
}
public void foo()
{
System.out.println("foo");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用“instance.foo();” 它将由主线程处理。
a 的想法Runnable是它是一段合并的代码,可以由它选择的任何上下文(在本例中为线程)内的其他内容执行。第二个线程将run()在启动时调用该方法,因此您可能希望foo()在您的MyClass.run()方法中调用。您不能从主线程中任意决定第二个线程现在将放弃它在run()方法中所做的一切并开始处理foo().
| 归档时间: |
|
| 查看次数: |
13030 次 |
| 最近记录: |