Results 1 to 5 of 5
- 12-15-2010, 07:42 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
Printing keyboard input to screen.
Ok so I have the following class:
Java Code:package module8; import java.io.*; public class KeyBoardThread implements Runnable { private Thread t; public KeyBoardThread(){} @Override public void run() { String s = null; while(t!=null){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { s = br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(s.contains("EXIT")){ stop(); } System.out.println(s); } } public void stop(){ //nullify the thread to cause any running method to stop t=null; } public void start(){ // starts new thread if(t==null){ t = new Thread(this,"KeyBoardThread"); t.start(); } } }
Java Code:KeyBoardThread testKeyBoardThread = new KeyBoardThread(); testKeyBoardThread.start(); testKeyBoardThread.run();
Anyone offer any reason why its doing this?
-
I have a question for you: why are you explicitly calling run() on this object?
- 12-15-2010, 09:08 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
The final module of my java course is on Threads / Graphics & Applets.
This is the first part of the exercises we were given. We have four threads running, one taking keyboard input, one reading a URL and printing the contents, one telling the time, one telling how much memory is being used.
-
You still need to evaluate why you are specifically calling run() on your KeyBoardThread Runnable object. Your code is effectively calling this method twice, once invoked by the Thread object when its start() method is called, and once directly called by you. Please Google and check out the Oracle tutorial on Threads for more on this, but I can't help but wonder if your directly calling this method is not messing you up.
- 12-16-2010, 11:40 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Read input from keyboard
By bison in forum New To JavaReplies: 2Last Post: 11-20-2010, 07:48 PM -
Help with on screen keyboard Capslock
By linkinlog0123 in forum AWT / SwingReplies: 2Last Post: 11-17-2010, 06:46 AM -
How to use another image using a keyboard input
By Rekuta in forum New To JavaReplies: 0Last Post: 05-13-2010, 06:00 PM -
get keyboard input while running in the background?
By gen1mx6 in forum Advanced JavaReplies: 16Last Post: 07-16-2009, 04:51 PM -
Need help with on-screen keyboard (first post)
By tom_k in forum New To JavaReplies: 7Last Post: 07-31-2008, 03:07 AM
Bookmarks