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");
}
}
}
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().