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...

[543 byte] By [juniorprogrammera] at [2007-11-27 5:54:51]
# 1

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.

puckstopper31a at 2007-7-12 15:49:44 > top of Java-index,Java Essentials,Java Programming...
# 2

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

juniorprogrammera at 2007-7-12 15:49:44 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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.

petes1234a at 2007-7-12 15:49:44 > top of Java-index,Java Essentials,Java Programming...
# 4

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?

juniorprogrammera at 2007-7-12 15:49:44 > top of Java-index,Java Essentials,Java Programming...
# 5

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 ;)....

apanousisa at 2007-7-12 15:49:44 > top of Java-index,Java Essentials,Java Programming...
# 6
Edit: Forget my answer, I guess I answered without thinking of the bigger picture.Message was edited by: Peetzore
Peetzorea at 2007-7-12 15:49:44 > top of Java-index,Java Essentials,Java Programming...