英文:
Cannot execute Synchronised thread
问题
我尝试了教科书上关于同步线程的代码。
尝试按原样编写代码。但是出现错误:
cannot find symbol f.start(); and t.display();
教科书上的代码应该在不使用synchronized
关键字的情况下尝试。
但是似乎编译器无法识别该对象。
请帮忙解决。。
class First
{
synchronized void display (String s)
{
System.out.println(s);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("***");
}
}
class Second implements Runnable
{
String s;
First f;
Thread t;
public Second(First f1,String s1)
{
f=f1;
s=s1;
t=new Thread(this);
t.start();
}
public void run()
{
f.display(s);
}
}
class SyncThread
{
public static void main(String args[])
{
First f=new First();
Second ob1=new Second(f,"First");
Second ob2=new Second(f,"Second");
Second ob3=new Second(f,"Third");
try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
}
英文:
I tried a code from Textbook about synchronized thread.
Tried writing the code as it is. But getting the error:
cannot find symbol f.start(); and t.display();
The textbook code was supposed to be tried without the synchronized keyword.
But it seems the compiler is unable to recognize the object.
Please help..
class First
{
synchronized void display (String s)
{
System.out.println(s);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("***");
}
}
class Second implements Runnable
{
String s;
First f;
Thread t;
public Second(First f1,String s1)
{
f=f1;
s=s1;
t=new Thread(this);
f.start();
}
public void run()
{
t.display(s);
}
}
class SyncThread
{
public static void main(String args[])
{
First f=new First();
Second ob1=new Second(f,"First");
Second ob2=new Second(f,"Second");
Second ob3=new Second(f,"Third");
try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
}
答案1
得分: 3
代替进行以下操作:
t = new Thread(this);
f.start();
尝试:
t = new Thread(this);
t.start();
在run
方法中,尝试使用f.display()
代替t.display()
看起来你正在搞乱一些变量。尝试使用一些有意义的名称,而不是f
和t
。使用thread.start()
和first.display()
比使用t.start()
和f.display()
更不容易出错。
英文:
Instead of doing
t = new Thread(this);
f.start();
try:
t = new Thread(this);
t.start();
and in the run
method, try f.display()
instead of t.display()
Looks like you're messing up some variables. Try using some meaningful names instead of f
and t
. It's more difficult to mess up thread.start()
and first.display()
than t.start()
and f.display()
.
答案2
得分: 0
Thread类没有display方法,而你的First类有display方法。我认为你错误地键入了t.display()和f.start()。尝试切换到f.display()和t.start()。
英文:
Thread class does not have display method and your class First has the display method. I think you have wrongly typed t.display() and f.start(). Try to swith to f.display() and t.start().
答案3
得分: 0
可能是因为你在run()
函数上面没有使用@Override
注解,所以它给你返回了一个错误。
这是修正后的代码:
@Override
public void run()
{
f.display(s);
}
还有,请不要在构造函数中运行线程,因为这样做是不安全的。另外,你还犯了一个错误,应该是t.start()
而不是f.start()
,在run()
函数中也有类似的改变,应该是f.display(s)
而不是t.display(s)
。
修改后的内容:
@Override
public void run()
{
f.display(s);
}
还有:
t = new Thread(this);
t.start();
记得检查你的变量,因为它们可能引起类似这样的问题!现在程序应该可以正常运行,不会有任何错误!
英文:
It is giving you an error possibly because you have not used the @Override annotation above 'run()' function.
Here is the revised code:
@Override
public void run()
{
f.display(s);
}
And Please do not run a thread in the constructor as it is not safe also you have made a mistake as it should be t.start() and not f.start(), similar changes in run()
should f.display(s) not t.display(s).
Changes:
@Override
public void run()
{
f.display(s);
}
And:
t=new Thread(this);
t.start();
Remember your variables because it would cause problems like these!
Now the program should run fine without any errors!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论