我是java的初学者。我一直在研究多线程。我想创建两个线程,这两个线程必须同时运行不同的方法。这里这些线程应该调用sum和diff方法并同时运行。但是我收到一个错误,该方法应该是线程类型。如何实现它。从线程调用函数
class Demo implements Runnable
{
void sum()
{
//Some lines of code
}
void diff()
{
//Some lines of code
}
public void run()
{
System.out.println("Inside run");
}
}
class Test
{
public static void main (String []args){
Demo o = new Demo();
Demo o1 = new Demo();
Thread th = new Thread (o);
Thread th1= new Thread(o1);
th.start();
th1.start();
o.th.sum(); // getting error here
o1.th1.diff(); // getting error here
}
}
Google? http://docs.oracle.com/javase/tutorial/essential/concurrency/simple.html – VolatileDream
格式化您的代码 – Anton
您需要从运行方法中调用各种方法。但是,如果你是java新手,多线程并不是开始冒险的好地方。 – nook