Java Reference
In-Depth Information
until Enter key pressed
System.out.println("Enter pressed...\n");
} catch (IOException e) {
// Handle IO
exception
System.out.println(e);
// Output
the exception
}
System.out.println("Ending main()");
return;
}
// Method where thread execution will start
@Override
public void run() {
try {
while(true) {
// Loop
indefinitely...
System.out.print(firstName);
// Output
first name
sleep(aWhile);
// Wait
aWhile msec.
System.out.print(secondName + "\n");
// Output
second name
}
} catch(InterruptedException e) {
// Handle
thread interruption
System.out.println(firstName + secondName + e);
// Output
the exception
}
}
private String firstName;
// Store
for first name
private String secondName;
// Store for
second name
private long aWhile;
// Delay in
milliseconds
}
TryThread.java
If you compile and run the code, you see something like this:
Press Enter when you have had enough...
Hopalong Marilyn Slim Cassidy
Hopalong Monroe
Marilyn Cassidy
Hopalong Pickens
Slim Monroe
Marilyn Cassidy
Hopalong Cassidy
Search WWH ::




Custom Search