• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with this code  RSS feed

 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting errors when I am trying to compile this program. I am not sure where have I done wrong.....

class Priority implements Runnable
{
Thread t;

public void run()
{for(int i=0;i<5;i++)
{
t=Thread.currentThread();
System.out.println("Thread.currentThread.getPriority="+t.getPriority());
System.out.println("Thread.currentThread.getName()="+t.getName());
try
{Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println();
}
}
}


}
class ThreadPrioritDemo
{
Priority pr=new Priority();
Thread tpr=new Thread(pr, "ThreadA");
System.out.println("in main() -Thread.currentThread().getName()="+Thread.currentThread.getName());
tpr.start();

try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
}
System.out.println("in main() -Thread.currentThread().getPriority()="+Thread.currentThread().getPriority());

}


I am getting following errors


ThreadPrioritDemo.java:29: <identifier> expected
System.out.println("in main() -Thread.currentThread().getName()="+Thread.cur
rentThread.getName());
^
ThreadPrioritDemo.java:30: <identifier> expected
tpr.start();
^
ThreadPrioritDemo.java:32: illegal start of type
try
^
ThreadPrioritDemo.java:39: <identifier> expected
System.out.println("in main() -Thread.currentThread().getPriority()="+Thread
.currentThread().getPriority());

^
4 errors
 
Roy Simon
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i have added comments to places where error are present....
class Priority implements Runnable
{
Thread t;

public void run()
{for(int i=0;i<5;i++)
{
t=Thread.currentThread();
System.out.println("Thread.currentThread.getPriority="+t.getPriority());
System.out.println("Thread.currentThread.getName()="+t.getName());
try
{Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println();
}
}
}


}
class ThreadPrioritDemo
{
Priority pr=new Priority();
Thread tpr=new Thread(pr, "ThreadA");
System.out.println("in main() -Thread.currentThread().getName()="+Thread.currentThread.getName()); //Thread.currentThread().getName()--- getCurrentThread() is a method//
tpr.start();//this has to be in the constructr or in a method---- it cant be outside methods or initializers
// the try catch too has to be in a method(constructor, static and non //static initializers or methods-- the try catch cant be placed outside //these in the class definition--- only instance variables can be declared //there!!)
try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
}
System.out.println("in main() -Thread.currentThread().getPriority()="+Thread.currentThread().getPriority());

}


the following is the sample of what may be that u need!!


class Priority implements Runnable
{
Thread t;

public void run()
{for(int i=0;i<5;i++)
{
t=Thread.currentThread();
System.out.println("Thread.currentThread.getPriority="+t.getPriority());
System.out.println("Thread.currentThread.getName()="+t.getName());
try
{Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println();
}
}
}


}
class ThreadPrioritDemo
{
public static void main(String[] a)
{
Priority pr=new Priority();
Thread tpr=new Thread(pr, "ThreadA");
System.out.println("in main() -Thread.currentThread().getName()="+Thread.currentThread.getName());
tpr.start();

try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
}
System.out.println("in main() -Thread.currentThread().getPriority()="+Thread.currentThread().getPriority());

}
}

try compiling and running the program now
If u still have doubts please post back here!!

Regards
Simon
[ April 01, 2006: Message edited by: Roy Simon ]
 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Simon...... I got it now.......
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!