problem with thread.sleep()
need help... where should i add a prompt for the user to say that there's no action done, if during the thread.sleep() the disk drive is not inserted?
bytheway how does the Thread.sleep(500); works?
the code are as follows:
public static void main(String[] args)
{
File f = new File("f:\\");
System.out.println("Insert disk drive...");
while (!f.exists())
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
}
.
.
.
thank you...
There are a couple of ways you could approach this.
The way that leaps to mind would be to create a runnable object that has a timer in it that's waiting a specified amount of time. If that thread exits before a disk has been found at that target location have it bail out or whatever your desired behavior at that point is.
PS.
public static void main(String[] args)
{
File f = new File("f:\\");
System.out.println("Insert disk drive...");
while (!f.exists())
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
}
.
.
.
what I mean is from the code above where should I modify and add the code " System.out.println("detection failed, please insert disk drive..."); " after about 30sec when user did not insert the disk drive?
thanks
> what I mean is from the code above where should I
> modify and add the code "
> System.out.println("detection failed, please insert
> disk drive..."); " after about 30sec when user did
> not insert the disk drive?
>
> thanks
I don't think that you can do it the way you are proposing, not with thread.sleep -- and I believe the other poster was telling you this as well. You need to learn the basics on multithreading in Java. Find a tutorial on this is your first step. After you study this, then come back if you have problems.
public static void main(String[] args)
{
File f = new File("f:\\");
System.out.println("Insert disk drive...");
while (!f.exists())
try {
Thread.sleep(0);
}
catch (InterruptedException e) {
}
.
.
.
the program above remain sleep forever when compiled till the disk drive is inserted,
why do the program still works when i put 0 milliseconds?
try this File file = new File("f:/");
Thread.sleep(500);
System.out.println("Insert disk drive...");
if(!file.exists()){
System.out.println("doesn't exist");
System.exit(-1);
}
first tell your program to wait for some seconds and then if these go by, then go into if clause ;)....