KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > io > MaskingThread


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util.io;
16
17 import java.io.*;
18
19 /**
20  * This class attempts to erase characters echoed to the console.
21  * @since 1.4
22  */

23 class MaskingThread extends Thread JavaDoc {
24    private volatile boolean stop;
25    //private char echochar = '*';
26

27   /**
28    * @param prompt The prompt displayed to the user
29    */

30    public MaskingThread(String JavaDoc prompt) {
31       System.out.print(prompt);
32    }
33
34   /**
35    * Begin masking until asked to stop.
36    */

37    public void run() {
38       int priority = Thread.currentThread().getPriority();
39       Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
40
41       try {
42          stop = true;
43          while(stop) {
44            System.out.print("\010 " /*+ echochar*/);
45            try {
46               // attempt masking at this rate
47
Thread.currentThread().sleep(1);
48            }catch (InterruptedException JavaDoc iex) {
49               Thread.currentThread().interrupt();
50               return;
51            }
52          }
53       } finally { // restore the original priority
54
Thread.currentThread().setPriority(priority);
55       }
56    }
57
58   /**
59    * Instruct the thread to stop masking.
60    */

61    public void stopMasking() {
62       this.stop = false;
63    }
64 }
65
Popular Tags