So i am trying to build a simple application to show concurrency in java. I wanna open 3 console windows so that i can get input output from all of them. How can do this?
Edit:I have 3 threads, each thread has its own set of cache. I wanna run 3 consoles separately in java.
i opened command prompt using Runtime class and tried attaching input and outputstream to it. But failed writing or reading from that newly opened console.
In most OSes, a process can open only one console.
You could create multiple processes and communicate between them, but that would show interprocess communication rather than inter-thread communication.
On Windows, you could try spawning child processes using 'more' or 'copy con' commands and writing to their input stream, or run your own java client which just echos input to output.
You could also create multiple JFrames and record the output of the threads in them, but again a given process has to do all its swing updates in the one swing thread, so the different threads have to sync when they output.
You're probably better off writing to the same console with different prefixes; output to the screen is always going to be a single thread in current OSes.
Related
I have a program made in Java, which you want to configure as an operating system service. The program reads data received by socket, which processes and stores it. It also logs errors and finally prints the information flow all the time in console to observe its operation.
After setting it up as a service, a need arose.
How do I view the print activity in console, if the application is in the background and therefore the console is not open?
Is there a way to view the console output of that application?
It occurred to me, if it is possible to create another Java application to read and print the console output of the created service. Would that solve the problem?
Thank you very much for your comments, after researching a little more thoroughly, according to your suggestions, i developed an IPC communication system via Sockets, in which, an swing application connects to the service and requests Every 2 seconds the required information and the sample in the app
Is it possible to create for only one process two different CLI (CommandLineInterface)?
I would like to have one CLI with my real program, and another CLI for a chat, so that i can send command to my program and send messages in the chat at the same time, and obviously have different views for the program and the chat.
(edit)
the program is a game and the chat is to make communication between all player, but when i start my program in eclipse, that program strat with only a console and here i would like to have two console in one there is the game with its action and state and in the other one i would like to have all the messages in the chat.
I know that i can build another process from zero and integrate it with process builder, but i would like to have all in one process.
If I assume that by CLI you mean the main terminal from where you execute your program, the answer is NO, regardless the OS.
There are couple of options to implement additional CLI interfaces in the same process:
listening on socket and waiting for client(s) to connect by e.g. telnet
opening a window that implements the CLI
under UNIX you can spawn e.g. a xterm and process its IO in your process
Under Linux or OSX, just open a new terminal window, and you will have an additional CLI to work with, and yes, you can try your program from those two different environments simultaneously and independently.
Under Windows, I couldn't say. You're probably using cygwin or something like that, so you should probably try to be a bit more specific in your question to get more attention.
I am running a Java process with several threads from a Powershell on Windows Server in Admin Mode.
Sometimes it randomly freezes until I press Ctrl+C, then it just picks up the work again.
Has anyone ever come across this problem and point me to where to look at?
If you start the process with Runtime.exec(..), it is sometimes necessary to read Bytes from the Process.getInputStream() resp. Process.getErrorStream(), else the process blocks, when it tries to write more to std-out (or std-err) than a certain buffer size.
I had this problem often, when starting Shell scripts.
You can create a Background Thread to read periodically from these streams.
Alternatively and more easy, you can use the ProcessBuilder class to start the shell process and use "inheritIO()" method.
I am writing a shell program in java, where it prompts to my user to enter input (i.e. "Prompt: ")
However separate threads that monitor activity also print to the shell, ex: "Client Connected!"
Any ideas/tips on how to make this a more user-friendly environment, where input from the user preceding a return would not be interrupted by a system message, but still allowing the message to display?
Typical console applications print information to screen sequentially. Programs the print information while waiting response to user are interactive program that usually control where and how do they print information. As an example you can see how top works on Unix.
There is a way to implement such kind of applications in java. You have to implement escape sequences for each supported terminal, so that you can control where and how to print each character. Fortunately there is such library that already does this.
See for example this discussion:
What's a good Java, curses-like, library for terminal applications?
I've a closed-sourced Windows XP GUI-based that performs some measurements.
My current manual workflow is:
Start the data capture by clicking on the "Run" button on the GUI
Stop the data capture by pressing the "Stop" button on the GUI.
Read some value displayed on the screen.
Save the data for the session to a file.
There is no library or API to automate this whole interaction and therefore I've no option but to do it manually :( and I'm sick of it !
As you clearly see that this approach is not only time-consuming but also error prone because it is limited by my reaction time which varies with every run.
I was wondering if there is a way to automate this interaction? If yes, what are my options? I would prefer to implement something in Python or Java. But I'm open to other options as well.
My idea is to implement a server process that runs on the Windows XP machine. I can then remotely send requests to this server process which in turn will execute my workflow automatically.
There's an amazing windows GUI automation tool called autoit. http://www.autoitscript.com/site/autoit/
You asked about linking AutoIt with Java. For my purposes, I've done this using a ProcessBuilder to create a Process, then get the Processes InputStream and ErrorStream and be sure to handle these streams in a separate thread. I have AutoIt communicate with my Java program using the InputStream. A good article for this (though a little out of date) is this one: When Runtime.exec() won't. It is key to be sure that the process be run on a background thread and that the two streams be read in their own threads. If you're doing this in a Swing GUI, then extra care must be taken that all Swing calls be made on the main Swing event thread, the EDT.