Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help required urgently  RSS feed

 
arun iyer
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made a quiz module just like java ranch. I have included a timer for each round. The timer is fine for the first round but when the next round starts the thread does not start again.
the next time i call the wastart() method, it does not go inside the "for loop".
Pls help me find the errrors.

public void wastop() {
b=false;

}
public void wastart() {
b=true;
t1.start();
}

public void run () {
i1=1;
System.out.println("Inside run()"+b);
for (int i=59;i>=0;i--)
{
if (b)
{
try
{
t1.sleep(1000);
} catch (InterruptedException e){}
}
if (i==0 && i1!=0)
{
i1=i1-1;
i=59;
}
str1 = "0:"+i1+":"+i;
repaint();

if (i==0 && i1==0)
{
quiz.myquizbook.ak();
repaint();
t1.stop();
}
}
}
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem may be conceptual rather than technical. Please note the fact that a thread cannot be restarted once it has exited the run() method.
 
arun iyer
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rahul,
thanks for the reply.
But what is the other alternative to start the timer?
pls suggest me something at the earliest.

arun iyer
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by arun iyer:
But what is the other alternative to start the timer?

Create a new Thread(Runnable).
- Peter

 
arun iyer
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply. I created a new thread and the timer is working but in the 2 round, timer starts but does not repaint() in the applet.
the code is as follows:
public void wastop() {
b=false;

}
public void wastart() {
b=true;
Thread t2 = new Thread();
t2.start();
}

public void run() {
i1=1;
System.out.println("Inside run()"+b);
for (int i=59;i>=0;i--)
{
if (b)
{
try
{
str1 = "0:"+i1+":"+i;
repaint();
Thread.sleep(1000);
}
catch (InterruptedException e){}

if (i==0 && i1!=0)
{
i1=i1-1;
i=59;
}

}

System.out.println("Before if");
if (i==0 && i1==0)
{
System.out.println("After if");
try
{
myQuizzer.pManager.ak();
Thread.sleep(100000);
}
catch (Exception e) {}


}
}
}
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Try checking out using notify(), notifyAll() and wait(). If your condition is not met then wait().
In a first scenario you could use the wait() method which also overloaded to take a long milliseconds as parameter.
In another scenario you could wait() in the thread and the timer thread will undertake the notification of notifyAll().

I hope this helps. Check out the Java Tutorial for an example of wait(), notify() and notifyAll()
Else check out if this helps. I have added while loop in your run method.



[This message has been edited by Rahul Mahindrakar (edited February 05, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!