Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to make a programme which does times tables and it outputs a to a file called log.txt as a log file, but when I run it all it does is runs though and does the times tables and makes the file, but writes nothing to the file. Can someone please tell me, what's wrong with my code? If you do, thanks.
Key:
[.jar = runnable file]
[.zip = source code]
Download links:- http://wardogsk93-ftp.bugs3.com/Downloads/Java/Counter/
Java Doc:- http://wardogsk93-ftp.bugs3.com/Downloads/Java/Counter/Java%20Doc/
Main.java:-
public class Main {
/************************************************/
/*************STUFF YOU CAN CHANGE***************/
/************************************************/
/** change this to start at a different number must be a number {#link Integer}**/
public static int minCount = 1;
/** change to change the number of where the programme will end must be a number {#link Integer}**/
public static int maxCount = 10;
/** change this to how many times you want to sleep for in seconds (1 = 1 second, 2 = 2 second, 10 = 10 second) before moving to next sum must be a number {#link Integer} **/
public static int sleepAmountMultiplyer = 1;
/** true = outputs to the command prompt / false = outputs to eclipse console {#link boolean}**/
public static boolean outputTOCMD = true;
/************************************************/
/******DONT CHANGE ANYTHING BELOW THIS LINE******/
/************************************************/
/** allows to output to a command prompt **/
private static Console cmd;
private static Output file;
private static int endNumber = maxCount + 1;
private static int sleepAmount = 1000 * sleepAmountMultiplyer;
/**
* main method
* call this to start
**/
public static void start() {
file = new Output();
if (outputTOCMD) {
cmd = new Console();
count();
cmd.exit();
} else {
count();
System.exit(1);
}
}
public static Main getInstance() {
return Main.getInstance();
}
/**code to start running**/
private static void count() {
try {
for (int i = minCount; i < maxCount + 1; i++) {
int j = i * i;
Thread.sleep(sleepAmount);
if (i == endNumber) {
return;
}
if (outputTOCMD) {
cmd.out(i + " X " + i + " = " + j);
file.write(String.valueOf(i) + " X " + String.valueOf(i) + " = " + String.valueOf(j));
} else {
System.out.println(i + " X " + i + " = " + j);
file.write(String.valueOf(i) + " X " + String.valueOf(i) + " = " + String.valueOf(j));
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Output.class:-
import java.io.*;
public class Output {
public Output() {}
private Console cmd;
private File logFile;
private String input;
private BufferedReader reader;
private BufferedWriter writer;
public Output(Console cmd, File logFile, BufferedReader reader, BufferedWriter writer) {
this.cmd = cmd;
this.logFile = logFile;
this.reader = reader;
this.writer = writer;
}
/** writes to a log file using {#link FileWriter} **/
public void write(String message) {
try {
logFile = new File("log.txt");
writer = new BufferedWriter(new FileWriter(logFile));
if (!logFile.exists()) {
writer.write(message);
writer.close();
} else {
read();
if (logFile.isFile()) {
logFile.delete();
writer.write(message);
}
}
} catch (IOException e) {
if (Main.outputTOCMD) {
cmd.out(e.getMessage());
} else {
e.printStackTrace();
}
}
}
/** writes to a log file using {#link FileReader} **/
public void read() {
try {
logFile = new File("log.txt");
reader = new BufferedReader(new FileReader(logFile));
if (logFile.exists()) {
setInput(reader.readLine());
}
} catch (IOException e) {
if (Main.outputTOCMD) {
cmd.out(e.getMessage());
} else {
e.printStackTrace();
}
}
}
/**
* #return the input
*/
private String getInput() {
return input;
}
/**
* #param input the input to set
*/
private String setInput(String input) {
this.input = input;
return input;
}
}
Console.class:-
import java.awt.Color;
import java.awt.Image;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTextArea;
/**
* Creates the command prompt window in class {#link Console}
*/
public class Console {
private JFrame frame;
private JTextArea console;
private Image icon;
public Console() {
try {
frame = new JFrame();
frame.setBackground(Color.BLACK);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setName("Commad Prompt");
frame.setSize(678, 340);
frame.setTitle(frame.getName());
frame.setVisible(true);
icon = new ImageIcon(new URL("http://upload.wikimedia.org/wikipedia/en/e/ef/Command_prompt_icon_(windows).png")).getImage();
frame.setIconImage(icon);
console = new JTextArea();
console.setAutoscrolls(true);
console.setBackground(Color.BLACK);
console.setEditable(false);
console.setForeground(Color.WHITE);
console.setSelectionColor(Color.WHITE);
console.setSelectedTextColor(Color.BLACK);
console.setVisible(true);
frame.add(console);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
/**
* #param {#link String} text does the same as {#link System}.out.println();
*/
public void out(String text) {
console.append(text + "\n");
}
/**
* #exception {#link Exception} to catch any errors and prints them to the window
* does the same has {#link System}.exit(1);
*/
public void exit() {
try {
Thread.sleep(1000 * Main.sleepAmountMultiplyer);
console.disable();
frame.dispose();
System.exit(1);
} catch (Exception e) {
this.out(e.getMessage());
}
}
/**
* #return Allows you to acces all the stuff in <br>{#link Console}</br>
**/
public Console getInstance() {
return this;
}
}
Launch File:-
public class Test {
public static void main(String[] args) {
Main.minCount = 1;
Main.maxCount = 10;
Main.sleepAmountMultiplyer = 1;
Main.outputTOCMD = true;
Main.start();
}
}
You must always use the close() method when finished writing to a file. Else wise it won't save it (you also want to close to avoid resource leak errors...read here). So in this method:
public void write(String message) {
try {
logFile = new File("log.txt");
writer = new BufferedWriter(new FileWriter(logFile));
if (!logFile.exists()) {
writer.write(message);
writer.close();
} else {
read();
if (logFile.isFile()) {
logFile.delete();
writer.write(message);
}
}
//close the buffer writer in order to save
writer.close();
} catch (IOException e) {
if (Main.outputTOCMD) {
cmd.out(e.getMessage());
} else {
e.printStackTrace();
}
}
}
Alternatively, you can close in a finally block. You must also close the BufferReader after you're done reading. You need to be very careful when using Thread if you plan to have multiple Thread reading/writing to same file.
NOTE: This will overwrite the file each time. However, if you want to append the data, change this line:
writer = new BufferedWriter(new FileWriter(logFile));
To:
writer = new BufferedWriter(new FileWriter(logFile, true));
The second parameter in FileWriter is confirming whether you want to overwrite the file or append to the file. Check out this example.
Related
so i have problem where i am trying to open a file through my own written virtual machine object in java but the problem is that the virtual machine is not running and causing an error.
Error: Could not find or load main class C:\Users\dell\Trace\Main.java
we also tried modifying the CLASSPATH & PATH with no success
below is the code of the main
import com.sun.jdi.Bootstrap;
import com.sun.jdi.connect.*;
import java.util.Map;
import java.util.List;
import java.util.Iterator;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Trace {
// Running remote VM
private final VirtualMachine vm;
// Thread transferring remote error stream to our error stream
private Thread errThread = null;
// Thread transferring remote output stream to our output stream
private Thread outThread = null;
// Mode for tracing the Trace program (default= 0 off)
private int debugTraceMode = 0;
// Do we want to watch assignments to fields
private boolean watchFields = false;
// Class patterns for which we don't want events
private String[] excludes = {"java.*", "javax.*", "sun.*",
"com.sun.*"};
/**
* main
*/
public static void main(String[] args) {
new Trace(args);
}
/**
* Parse the command line arguments.
* Launch target VM.
* Generate the trace.
*/
Trace(String[] args) {
PrintWriter writer = new PrintWriter(System.out);
int inx;
for (inx = 0; inx < args.length; ++inx) {
String arg = args[inx];
if (arg.charAt(0) != '-') {
break;
}
if (arg.equals("-output")) {
try {
writer = new PrintWriter(new FileWriter(args[++inx]));
} catch (IOException exc) {
System.err.println("Cannot open output file: " + args[inx]
+ " - " + exc);
System.exit(1);
}
} else if (arg.equals("-all")) {
excludes = new String[0];
} else if (arg.equals("-fields")) {
watchFields = true;
} else if (arg.equals("-dbgtrace")) {
debugTraceMode = Integer.parseInt(args[++inx]);
} else if (arg.equals("-help")) {
usage();
System.exit(0);
} else {
System.err.println("No option: " + arg);
usage();
System.exit(1);
}
}
if (inx >= args.length) {
System.err.println("<class> missing");
usage();
System.exit(1);
}
StringBuffer sb = new StringBuffer();
sb.append(args[inx]);
for (++inx; inx < args.length; ++inx) {
sb.append(' ');
sb.append(args[inx]);
}
vm = launchTarget(sb.toString());
generateTrace(writer);
}
/**
* Generate the trace.
* Enable events, start thread to display events,
* start threads to forward remote error and output streams,
* resume the remote VM, wait for the final event, and shutdown.
*/
void generateTrace(PrintWriter writer) {
vm.setDebugTraceMode(debugTraceMode);
EventThread eventThread = new EventThread(vm, excludes, writer);
eventThread.setEventRequests(watchFields);
eventThread.start();
redirectOutput();
vm.resume();
// Shutdown begins when event thread terminates
try {
eventThread.join();
errThread.join(); // Make sure output is forwarded
outThread.join(); // before we exit
} catch (InterruptedException exc) {
// we don't interrupt
}
writer.close();
}
/**
* Launch target VM.
* Forward target's output and error.
*/
VirtualMachine launchTarget(String mainArgs) {
LaunchingConnector connector = findLaunchingConnector();
Map arguments = connectorArguments(connector, mainArgs);
try {
return connector.launch(arguments);
} catch (IOException exc) {
throw new Error("Unable to launch target VM: " + exc);
} catch (IllegalConnectorArgumentsException exc) {
throw new Error("Internal error: " + exc);
} catch (VMStartException exc) {
throw new Error("Target VM failed to initialize: " +
exc.getMessage());
}
}
void redirectOutput() {
Process process = vm.process();
// Copy target's output and error to our output and error.
errThread = new StreamRedirectThread("error reader",
process.getErrorStream(),
System.err);
outThread = new StreamRedirectThread("output reader",
process.getInputStream(),
System.out);
errThread.start();
outThread.start();
}
/**
* Find a com.sun.jdi.CommandLineLaunch connector
*/
LaunchingConnector findLaunchingConnector() {
List connectors = Bootstrap.virtualMachineManager().allConnectors();
Iterator iter = connectors.iterator();
while (iter.hasNext()) {
Connector connector = (Connector)iter.next();
if (connector.name().equals("com.sun.jdi.CommandLineLaunch")) {
return (LaunchingConnector)connector;
}
}
throw new Error("No launching connector");
}
/**
* Return the launching connector's arguments.
*/
Map connectorArguments(LaunchingConnector connector, String mainArgs) {
Map arguments = connector.defaultArguments();
Connector.Argument mainArg =
(Connector.Argument)arguments.get("main");
if (mainArg == null) {
throw new Error("Bad launching connector");
}
mainArg.setValue(mainArgs);
if (watchFields) {
// We need a VM that supports watchpoints
Connector.Argument optionArg =
(Connector.Argument)arguments.get("options");
if (optionArg == null) {
throw new Error("Bad launching connector");
}
optionArg.setValue("-classic");
}
return arguments;
}
/**
* Print command line usage help
*/
void usage() {
System.err.println("Usage: java Trace <options> <class> <args>");
System.err.println("<options> are:");
System.err.println(
" -output <filename> Output trace to <filename>");
System.err.println(
" -all Include system classes in output");
System.err.println(
" -help Print this help message");
System.err.println("<class> is the program to trace");
System.err.println("<args> are the arguments to <class>");
}
}
I have a Java program that is using threads to transfer data through sockets. The issue I am having is in my server manager when I create a thread pool. If for instance I create a thread-pool of 3, waiting for 3 clients to connect, and then manually run CapitalizeClient.java 3 times, everything works fine and I can manage threads/close sockets perfectly fine, but if I create that same thread-pool and then automate the creating of CapitalizeClient.java by instantiating the class within a loop, I run into a huge issue, being that when I go to close a socket every socket in my entire program closes and everything shuts down.
Here is the class that's giving me trouble
package parallel_hw_6;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CapitalizeClient {
private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame("Capitalize Client");
private JTextField dataField = new JTextField(40);
private JTextArea messageArea = new JTextArea(8, 60);
private String name;
private JTextArea log = new JTextArea(20,20);
/**
* Constructs the client by laying out the GUI and registering a
* listener with the textfield so that pressing Enter in the
* listener sends the textfield contents to the server.
*/
public CapitalizeClient() {
// Layout GUI
messageArea.setEditable(false);
frame.getContentPane().add(dataField, "North");
frame.getContentPane().add(new JScrollPane(messageArea), "Center");
frame.getContentPane().add(new JScrollPane(log), "East");
// Add Listeners
dataField.addActionListener(new ActionListener() {
/**
* Responds to pressing the enter key in the textfield
* by sending the contents of the text field to the
* server and displaying the response from the server
* in the text area. If the response is "." we exit
* the whole application, which closes all sockets,
* streams and windows.
*/
public void actionPerformed(ActionEvent e) {
out.println(dataField.getText());
}
});
}
/**
* Implements the connection logic by prompting the end user for
* the server's IP address, connecting, setting up streams, and
* consuming the welcome messages from the server. The Capitalizer
* protocol says that the server sends three lines of text to the
* client immediately after establishing a connection.
*/
public void connectToServer() throws IOException {
// Get the server address from a dialog box.
String serverAddress = JOptionPane.showInputDialog(
frame,
"Enter IP Address of the Server:",
"Welcome to the Capitalization Program",
JOptionPane.QUESTION_MESSAGE);
// Make connection and initialize streams
Socket socket = new Socket(serverAddress, 9898);
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Consume the initial welcoming messages from the server
for (int i = 0; i < 3; i++) {
messageArea.append(in.readLine() + "\n");
System.out.println("initial message: "+i);
}
name = in.readLine();
Timer timer = new Timer();
TimerTask serverHeartbeat = new TimerTask() {
#Override
public void run() {
out.println(name+"_IS_ALIVE");
}
};
TimerTask threadHeartbeat = new TimerTask() {
#Override
public void run() {
out.println(name+"_ALIVE");
}
};
timer.schedule(serverHeartbeat, 1000, 1000);
timer.schedule(threadHeartbeat, 5000, 5000);
String response;
try {
while((response = in.readLine()) != null){
System.out.println("The input is: " + response);
Matcher m2 = Pattern.compile("([\\w]*)_ALIVE").matcher(response);
if (response == null || response.equals("")) {
System.exit(0);
}else if("CLEAR_CONSOLE".equals(response)){
messageArea.setText(null);
}else if(m2.matches()){
log.append(response+"\n");
}else{
messageArea.append(response + "\n");
dataField.selectAll();
}
}
if (response == null || response.equals("")) {
System.exit(0);
}
} catch (IOException ex) {
response = "Error: " + ex;
}
}
/**
* Runs the client application.
*/
public static void main(String[] args) throws Exception {
CapitalizeClient client = new CapitalizeClient();
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.pack();
client.frame.setVisible(true);
client.connectToServer();
}
}
Here is how I'm trying to instantiate n = thread-pool-size instances of the class to connect
for(int i = 0; i < numClients; i++){
(new Thread() {
#Override
public void run() {
try {
CapitalizeClient.main(null);
} catch (Exception ex) {
Logger.getLogger(CapitalizeServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}).start();
}
Note: When I instantiate multiple instances of the class this way they DO show up and connect properly as well as allow me to send messages to all of them or even to specific ones, it's just that when I go to close the socket down, ending the connection to one of the clients, it shuts all the clients down and then the server manager.
Here is a copy of the server manager code for reference.
package parallel_hw_6;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CapitalizeServer {
private static ArrayList<Capitalizer> allClients = new ArrayList<Capitalizer>();
private static ServerSocket listener;
private static ServerSocket listener2;
private static ExecutorService pool = Executors.newCachedThreadPool();
private static int threadPoolNum = 0;
private static Settings s;
private static RunIt runIt;
/**
* Application method to run the server runs in an infinite loop listening
* on port 9898. When a connection is requested, it spawns a new thread to
* do the servicing and immediately returns to listening. The server keeps a
* unique client number for each client that connects just to show
* interesting logging messages. It is certainly not necessary to do this.
*/
public static void main(String[] args) throws Exception {
System.out.println("The capitalization server is running.");
int clientNumber = 0;
int settingsClientNumber = 0;
listener = new ServerSocket(9898);
listener2 = new ServerSocket(9899);
// new Settings(listener2.accept(), settingsClientNumber++).start();
s = new Settings(listener2.accept(), settingsClientNumber++);
s.start();
// threadPool(2);
runIt = new RunIt();
try {
while (true) {
// new Capitalizer(listener.accept(), clientNumber++).start();
Capitalizer c = new Capitalizer(listener.accept(), clientNumber++, false, s);
Thread t = new Thread(c);
t.start();
}
} finally {
listener.close();
}
}
/**
* A private thread to handle capitalization requests on a particular
* socket. The client terminates the dialogue by sending a single line
* containing only a period.
*/
private static class Capitalizer implements Runnable {
private Socket socket;
private int clientNumber;
private String name;
private BufferedReader in;
private PrintWriter out;
private boolean fromThreadPool;
private Settings admin;
// private PrintWriter adminOut;
public Capitalizer(Socket socket, int clientNumber, boolean fromWhere, Settings admin) {
this.admin = admin;
// this.setName("Thread" + clientNumber);
this.fromThreadPool = fromWhere;
if (this.fromThreadPool == true) {
this.name = "Threadpool_Thread_" + clientNumber;
} else {
this.name = "Standard_Thread_" + clientNumber;
}
this.socket = socket;
this.clientNumber = clientNumber;
log("\nNew connection with client# " + clientNumber + " at " + socket);
allClients.add(this);
System.out.print("\n" + allClients);
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// adminOut = new PrintWriter(adminSocket.getOutputStream(), true);
} catch (Exception e) {
}
}
/**
* Services this thread's client by first sending the client a welcome
* message then repeatedly reading strings and sending back the
* capitalized version of the string.
*/
public void run() {
try {
// Decorate the streams so we can send characters
// and not just bytes. Ensure output is flushed
// after every newline.
// Send a welcome message to the client.
out.println("Hello, your name is " + name + ".");
out.println("Enter a line with only a period to quit\n");
out.println(name);
// Get messages from the client, line by line; return them
// capitalized
while (true) {
String input = in.readLine();
// System.out.println("\nInput check: "+ input);
Matcher m = Pattern.compile("([\\w]*)_IS_ALIVE").matcher(input);
Matcher m2 = Pattern.compile("([\\w]*)_ALIVE").matcher(input);
if (input == null || input.equals(".")) {
break;
} else if (m.matches()) {
admin.showHeartbeat(input);
} else if (m2.matches()) {
heartbeatToAll(input, this.name);
} else {
out.println(input.toUpperCase());
}
// out.println(input.toUpperCase());
}
} catch (IOException e) {
log("Error handling client# " + clientNumber + ": " + e);
} finally {
try {
socket.close();
} catch (IOException e) {
log("Couldn't close a socket, what's going on?");
}
log("Connection with client# " + clientNumber + " closed");
}
}
private void sendMessage(String message) {
try {
out.println(message);
} catch (Exception e) {
log("Error: Message could not be sent");
}
}
private void terminate() {
try {
socket.close();
} catch (Exception e) {
log("Couldn't close a socket, what's going on?");
}
}
/**
* Logs a simple message. In this case we just write the message to the
* server applications standard output.
*/
private void log(String message) {
System.out.println(message);
}
}
private static void threadPool(int numClients) {
try {
Thread waitForConnection = new Thread() {
#Override
public void run() {
try {
for (int i = 0; i < numClients; i++) {
System.out.print("Threadpool" + pool);
Capitalizer c = new Capitalizer(listener.accept(), threadPoolNum++, true, s);
pool.execute(c);
}
} catch (IOException ex) {
Logger.getLogger(CapitalizeServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
waitForConnection.start();
System.out.println("\nInside threadPool() - Number of threads in pool is " + numClients);
// Works but closes all if you close one
for (int i = 0; i < numClients; i++) {
(new Thread() {
#Override
public void run() {
try {
CapitalizeClient.main(null);
// CapitalizeClient test = new CapitalizeClient();
// test.main(null);
} catch (Exception ex) {
Logger.getLogger(CapitalizeServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}).start();
}
} catch (Exception ex) {
Logger.getLogger(CapitalizeServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void heartbeatToAll(String message, String name) {
String m = message;
String n = name;
for (int i = 0; i < allClients.size(); i++) {
String cName = allClients.get(i).name;
if (!cName.equals(n)) {
allClients.get(i).sendMessage(message);
}
}
}
private static class Settings extends Thread {
private Socket socket;
private int clientNumber;
private static PrintWriter out;
public Settings(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
log("\nNew connection with settings client# " + clientNumber + " at " + socket);
}
/**
* Services this thread's client by first sending the client a welcome
* message then repeatedly reading strings and sending back the
* capitalized version of the string.
*/
public void run() {
try {
// Decorate the streams so we can send characters
// and not just bytes. Ensure output is flushed
// after every newline.
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Send a welcome message to the client.
out.println("Hello, you are admin client #" + clientNumber + ".");
out.println("Enter a line with only a period to quit\n");
// Get messages from the client, line by line; return them
// capitalized
while (true) {
String input = in.readLine();
int intInput = 0;
Matcher m = Pattern.compile("([\\w]*)_IS_ALIVE").matcher(input);
if (input == null || input.equals(".")) {
break;
} else if (m.matches()) {
out.println(input);
break;
} else {
try {
intInput = Integer.parseInt(input);
System.out.print("\nintInput: " + intInput);
switch (intInput) {
case (1):
out.println("Which thread should I terminate?");
for (int i = 0; i < allClients.size(); i++) {
String toAdd = i + " : " + allClients.get(i).name;
out.println(toAdd);
}
while (true) {
String choice1 = in.readLine();
try {
int intChoice1 = Integer.parseInt(choice1);
System.out.print("\nintChoice1: " + intChoice1);
allClients.get(intChoice1).terminate();
out.println("CLEAR_CONSOLE");
out.println("Client number " + intChoice1 + " has been terminated");
if (allClients.get(intChoice1).fromThreadPool == true) {
threadPool(1);
}
allClients.remove(intChoice1);
System.out.println("\nall clients: " + allClients);
break;
} catch (Exception e) {
out.println("Enter a valid client number.");
log(e.toString());
}
}
break;
case (2):
out.println("Send to one or all?");
out.println("1. Choose 1");
out.println("2. Send to all");
while (true) {
String allOrOne = in.readLine();
try {
int intAllOrOne = Integer.parseInt(allOrOne);
System.out.print("\nintAllOrOne: " + intAllOrOne);
switch (intAllOrOne) {
case (1):
out.println("CLEAR_CONSOLE");
out.println("Which do you want to send to?");
for (int i = 0; i < allClients.size(); i++) {
String toAdd = Integer.toString(allClients.get(i).clientNumber);
String toPrint = i + " : " + allClients.get(i).name;
out.println(toPrint);
}
while (true) {
String sendToWho = in.readLine();
try {
int intSendToWho = Integer.parseInt(sendToWho);
System.out.print("\nintsendToWho: " + intSendToWho);
out.println("CLEAR_CONSOLE");
out.println("Enter message to send.");
while (true) {
String message = in.readLine();
System.out.print("\nmessage: " + message);
try {
allClients.get(intSendToWho).sendMessage(message);
break;
} catch (Exception e) {
out.print("Enter a valid client number.");
log(e.toString());
}
}
out.println("CLEAR_CONSOLE");
break;
} catch (Exception e) {
out.println("Enter a valid choice.");
log(e.toString());
}
}
break;
case (2):
out.println("CLEAR_CONSOLE");
out.println("Enter message to send test.");
while (true) {
String message = in.readLine();
System.out.print("\nmessage: " + message);
try {
for (int i = 0; i < allClients.size(); i++) {
allClients.get(i).sendMessage(message);
}
out.println("CLEAR_CONSOLE");
break;
} catch (Exception e) {
out.print("Enter a valid client number.");
log(e.toString());
}
}
break;
}
break;
} catch (Exception e) {
log(e.toString());
}
}
break;
case (3):
out.println("How many threads in the pool?");
while (true) {
String numThreads = in.readLine();
try {
int intNumThreads = Integer.parseInt(numThreads);
System.out.print("\nintNumThreads: " + intNumThreads);
out.println("CLEAR_CONSOLE");
out.println("Thread pool with" + intNumThreads + " has been created!");
out.println("Spawn new instance(s) of 'CapitalizeClient' class to utilize it.");
threadPool(intNumThreads);
break;
} catch (Exception e) {
out.println("Enter a valid number.");
log(e.toString());
}
}
break;
default:
out.println("Please enter a valid option.");
break;
}
} catch (Exception e) {
System.out.print("\nnumber format exception\n");
out.println("Please enter a valid option.");
}
}
}
} catch (IOException e) {
log("Error handling client# " + clientNumber + ": " + e);
} finally {
System.out.print("FINALLY IS EXECUTING!");
try {
socket.close();
} catch (IOException e) {
log("Couldn't close a socket, what's going on?");
}
log("Here?Connection with client# " + clientNumber + " closed");
}
}
private static void showHeartbeat(String h) {
out.println(h);
}
/**
* Logs a simple message. In this case we just write the message to the
* server applications standard output.
*/
private void log(String message) {
System.out.println(message);
}
}
}
I am currently programming a simple application that takes input from a midi device and outputs the corresponding sound. I have gotten everything to work now, but there is two problems: First of all, when i plug in a midi device AFTER the program has started or disconnect it while the program is running, it will not get recognized. Second of all, the latency is very high on OSX, Linux (Raspbian) and Windows alike. Does anyone know the solution to either of these issues?
Here is what i have so far:
The MidiHandler class reads the input and synthesizes the sound
public class MidiHandler {
MidiDevice device;
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
List<Transmitter> transmitters;
MidiInputReceiver reciever;
public MidiHandler() {
for (int i = 0; i < infos.length; i++) {
try {
this.device = MidiSystem.getMidiDevice(this.infos[i]);
// does the device have any transmitters?
// if it does, add it to the device list
System.out.println(this.infos[i]);
// get all transmitters
this.transmitters = this.device.getTransmitters();
// using my own MidiInputReceiver
this.reciever = new MidiInputReceiver(this.device
.getDeviceInfo().toString());
// and for each transmitter
for (int j = 0; j < this.transmitters.size(); j++) {
// create a new receiver
this.transmitters.get(j).setReceiver(this.reciever);
}
Transmitter trans = this.device.getTransmitter();
trans.setReceiver(new MidiInputReceiver(this.device
.getDeviceInfo().toString()));
this.device.open();
} catch (MidiUnavailableException e) {
}
}
}
public void playNote(byte b) {
reciever.playNote(b);
}
public void stopNote(byte b) {
reciever.stopNote(b);
}
public void close() {
for (int i = 0; i < this.transmitters.size(); ++i) {
this.transmitters.get(i).close();
}
this.reciever.close();
this.device.close();
}
public String getInfos() {
String infos = "";
for (int i = 0; i < this.infos.length; i++) {
infos += "\n" + this.infos[i] + " ";
}
return infos;
}
// tried to write my own class. I thought the send method handles an
// MidiEvents sent to it
public class MidiInputReceiver implements Receiver {
Synthesizer synth;
MidiChannel[] mc;
Instrument[] instr;
int instrument;
int channel;
public MidiInputReceiver(String name) {
try {
patcher p = new patcher();
this.instrument = p.getInstrument();
this.channel = p.getChannel();
this.synth = MidiSystem.getSynthesizer();
this.synth.open();
this.mc = synth.getChannels();
instr = synth.getDefaultSoundbank().getInstruments();
this.synth.loadInstrument(instr[1]);
mc[this.channel].programChange(0, this.instrument);
} catch (MidiUnavailableException e) {
e.printStackTrace();
System.exit(1);
}
}
public void send(MidiMessage msg, long timeStamp) {
/*
* Use to display midi message
for (int i = 0; i < msg.getMessage().length; i++) {
System.out.print("[" + msg.getMessage()[i] + "] ");
}
System.out.println();
*/
if (msg.getMessage()[0] == -112) {
mc[this.channel].noteOn(msg.getMessage()[1],
msg.getMessage()[2] + 1000);
}
if (msg.getMessage()[0] == -128) {
mc[this.channel].noteOff(msg.getMessage()[1],
msg.getMessage()[2] + 1000);
}
}
public void playNote(byte b) {
mc[this.channel].noteOn(b, 1000);
}
public void stopNote(byte b) {
mc[this.channel].noteOff(b);
}
public void close() {
}
}
The Shell class contains the main method
public class Shell {
private static MidiHandler midi;
AudioServer server;
private Shell() {
}
/**
* The main method. Executes via BufferedReader, to communicate with the
* user
*
* #param args
* Command line parameter
* #throws IOException
* IOExeption can occur
*/
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
execute(in);
}
/**
* Method used for general interaction with the user. Utilizes a
* BufferedReader to read inputs, then proceeds to analyze the input for
* predefined commands
*
* #param in
* The BufferedReader in use
* #throws IOException
* #throws ParseException
*/
private static void execute(BufferedReader in) throws IOException {
boolean quit = false;
while (!quit) {
midi = new MidiHandler();
System.out.print("midi> ");
String input;
input = in.readLine(); // read line
if (input == null | input.isEmpty()) {
break;
} // No command given?
// First character is always the command.
char command = Character.toLowerCase(input.charAt(0));
try {
switch (command) {
case 'n':
midi = new MidiHandler();
break;
case 'c':
midi.close();
break;
case 'p':
midi.playNote((byte) 60);
break;
case 's':
midi.stopNote((byte) 60);
case 'q':
midi.close();
quit = true;
System.exit(0);
break;
default:
System.out.println("Unknown command");
break;
}
} catch (NumberFormatException e) {
System.out.println("Invalid parameter");
}
}
}
Any help would be greatly appreciated. Thanks in advance.
I have Vector of threads, and i wanna check all items in this vector. Everyone item is connection of user to server. I wanna "clean" all dead connections.
I can't find where I'm wrong.
Here is it my code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package server;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import server.ServerCore.Clients;
/**
*
* #author pisio
*/
public class GrimReaper extends Thread {
private int timeout = LoadSettings.Init().getConfigInt("grimreaper") * 1000; // 1000 ms = 1 sec
public GrimReaper() {
super();
}
public void cleanUserThreads() {
Vector users = ServerCore.users;
if (users.size() < 1) {
return;
}
Iterator iteratr = users.iterator();
while (iteratr.hasNext()) {
Clients user = (Clients) iteratr.next();
System.out.println(user.isAlive());
if (user.getClient().isClosed()) {
user.interrupt();
if (user.isInterrupted()) {
System.out.println("Beshe kiknat");
}
iteratr.remove();
// if (PublicVaribles.Init().systemLevelMesseging() == 2) {
System.out.println("+\t Kicked user ");
// }
}//if is dead
}//while
}//cleanUserThreads;
#Override
public void run() {
try {
while (ServerCore.getServerRunning()) {
cleanUserThreads();
sleep(timeout);
System.out.println("nani na shinigami");
}
} catch (InterruptedException ex) {
Logger.getLogger(GrimReaper.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
package server;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import server.DB.DataBase;
public class ServerCore extends Thread {
private static ServerCore sc = null;
private int port = LoadSettings.Init().getConfigInt("port");
private int max_connections = LoadSettings.Init().getConfigInt("max_connections");
private String ipServer = LoadSettings.Init().getConfig("ipServer");
private ServerSocket socket;
private static boolean serverRuning = false;
public static Vector users = new Vector();
public GrimReaper shinigami = new GrimReaper();// Shinigami from Japanice is Grim Reaper!
private ServerCore() {
}
#Override
public void run() {
shinigami.start();
try {
socket = new ServerSocket(port, max_connections);
System.out.println("+++\t Server was started at address:" + socket.getLocalSocketAddress() + " with posible max users " + max_connections);
serverRuning = true;
while (serverRuning) {
Socket client = socket.accept();
shinigami.cleanUserThreads();
if (users.size() < max_connections) {
Clients cls = new Clients(client);
cls.start();
users.add(cls);
System.out.println("++\tClient was connected:" + client.toString());
} else {
Clients cls = new Clients(client);
cls.start();
cls.getOutput().println("sorry_no_avable_slot");
cls.getOutput().flush();
cls.interrupt();
}
}
} catch (IOException ex) {
// Logger.getLogger(ServerCore.class.getName()).log(Level.SEVERE, null, ex);
}
}
//run method
public void sendUserMsg() {
Scanner input = PublicVaribles.Init().inputKeyBord();
System.out.print("Enter UserID/user connection port:");
int userID = input.nextInt();
Iterator iterator = users.iterator();
while (iterator.hasNext()) {
Clients cls = (Clients) iterator.next();
/// System.out.println("Passed user:" + cls.getUserId());
if (cls.getUserId() == userID) {
System.out.print("\nEnter msg:");
String str = input.next();
cls.getOutput().println(str);
System.out.println("+\t" + cls.getUserId() + " get msg :" + str);
}
}
}
//SendUserMsg
public void stopServer() {
statusServer();
serverRuning = false;
try {
socket.close();
} catch (IOException ex) {
Logger.getLogger(ServerCore.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("+++\t SERVER WAS STOPED !");
}
//Stop server
public void statusServer() {
if (serverRuning) {
System.out.println("+++\t Server running at port:" + port + " with connected users :" + users.size() + "/" + max_connections);
} else {
System.out.println("+++\t Server IS NOT RUNNING!");
}
}
//Status server
public static boolean getServerRunning() {
// function for GrimReaper .... R.I.P :D
return ServerCore.serverRuning;
}
public static ServerCore Init() {
if (ServerCore.sc == null) {
ServerCore.sc = new ServerCore();
}
return ServerCore.sc;
}
// SingleTon
public class Clients extends Thread {
private Socket client;
private int userID;
private Scanner input;
private PrintWriter output;
public Clients(Socket socket) {
client = socket;
userID = socket.getPort();
try {
input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream(), true);
} catch (IOException ioEx) {
System.out.println(ioEx.toString());
}
}
public int getUserId() {
return userID;
}
public Scanner getInput() {
return input;
}
public PrintWriter getOutput() {
return output;
}
public Socket getClient() {
return client;
}
}//Clients Class
}
Note: I'm assuming Clients extends Thread.
It looks like you might be using interrupt() incorrectly. After calling user.interrupt(), it's up to that thread to check that it has been interrupted by calling Thread.interrupted() and terminate itself if true. Here's an extremely basic example:
class Clients extends Thread {
#Override
public void run() {
while (!Thread.interrupted()) {
//do work
}
//thread has been interrupted and quits
}
}
I have a very simple crawler. I want to make my current code run in a few threads. Could you provide me a little tutorial or article to help me achive this test?
I'm originally a .Net developer and in .Net I have no problem whatsoever running codes in multithread but unfortunately I don't know anything about threads in Java.
My crawler is a command-line software so don't worry about GUI.
Thank you in advance.
Java does multithreading through the Thread class. One of the most common ways to make existing code multithreaded is to use the Runnable interface to define what you want to call at thread start, and then start it off.
public class SomeFunctions
{
public static void FunctionA() {}
public static void FunctionB() {}
public static void FunctionC() {}
}
// ...
Thread t1 = new Thread(new Runnable() {
public void run() {
SomeFunctions.FunctionA();
}
});
t1.start();
// (rinse and repeat for the other functions)
Dry coded, but it should at least get the general concept across. Of course, as soon as you go into multithreading land, you have concurrency issues and need to make sure everything is appropriately syhchronized, etc., but any language will have those issues.
If you're worried about synchronization, you have a few tools at your disposal. The easiest is the recursive mutex functionality built into Java, the "synchronized" keyword. More classical means are also available through various classes in the java.util.concurrent and java.util.concurrent.locks packages such as Semaphore and ReadWriteLock
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/locks/package-summary.html
You can take a look at my webcrawler example. Sry for the lengthiness.
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* A web crawler with a Worker pool
*
* #author Adriaan
*/
public class WebCrawler implements Manager {
private Set<Worker> workers = new HashSet<Worker>();
private List<String> toCrawl = new ArrayList<String>();
private Set<String> crawled = new HashSet<String>();
private Set<String> hosts = new HashSet<String>();
private Set<String> results = new HashSet<String>();
private int maxResults;
public WebCrawler(String url, int numberOfWorkers, int maxResults) {
this.maxResults = maxResults;
toCrawl.add(url);
createWorkers(numberOfWorkers);
}
public void createWorkers(int numberOfWorkers) {
for (int i = 0; i < numberOfWorkers; i++) {
workers.add(new Worker(this));
}
}
private void stopWorkers() {
for (Worker worker : workers) {
worker.terminate();
}
}
public synchronized Job getNewJob() {
while (toCrawl.size() == 0) {
try {
wait();
} catch (InterruptedException e) {
// ignore
}
}
return new EmailAddressCrawlJob().setDescription(toCrawl.remove(0));
}
public synchronized void jobCompleted(Job job) {
// System.out.println("crawled: " + job.getDescription());
crawled.add(job.getDescription());
String host = getHost(job.getDescription());
boolean knownHost = hosts.contains(host);
if (!knownHost) {
System.out.println("host: " + host);
hosts.add(host);
}
for (String url : job.getNewDescriptions()) {
if (!crawled.contains(url)) {
if (knownHost) {
toCrawl.add(toCrawl.size() - 1, url);
} else {
toCrawl.add(url);
}
}
}
for (String result : job.getResults()) {
if (results.add(result)) {
System.out.println("result: " + result);
}
}
notifyAll();
if (results.size() >= maxResults) {
stopWorkers();
System.out.println("Crawled hosts:");
for (String crawledHost : hosts) {
System.out.println(crawledHost);
}
Set<String> uncrawledHosts = new HashSet<String>();
for (String toCrawlUrl : toCrawl) {
uncrawledHosts.add(getHost(toCrawlUrl));
}
System.out.println("Uncrawled hosts:");
for (String unCrawledHost : uncrawledHosts) {
System.out.println(unCrawledHost);
}
}
if (crawled.size() % 10 == 0) {
System.out.println("crawled=" + crawled.size() + " toCrawl="
+ toCrawl.size() + " results=" + results.size() + " hosts="
+ hosts.size() + " lastHost=" + host);
}
}
public String getHost(String host) {
int hostStart = host.indexOf("://") + 3;
if (hostStart > 0) {
int hostEnd = host.indexOf("/", hostStart);
if (hostEnd < 0) {
hostEnd = host.length();
}
host = host.substring(hostStart, hostEnd);
}
return host;
}
public static void main(String[] args) throws MalformedURLException {
new WebCrawler("http://www.nu.nl/", 5, 20);
}
}
Worker
**
* A Worker proactively gets a Job, executes it and notifies its manager that
* the Job is completed.
*
* #author Adriaan
*/
public class Worker extends Thread {
private final Manager manager;
private Job job = null;
private boolean isWorking;
public Worker(Manager manager) {
this.manager = manager;
isWorking = true;
start();
}
#Override
public void run() {
System.out.println("Worker " + Thread.currentThread().getId()
+ " starting ");
while (isWorking) {
job = manager.getNewJob();
job.execute();
manager.jobCompleted(job);
}
}
public void terminate() {
isWorking = false;
}
}
Manager interface
/**
* Manager interface for Workers
*
* #author Adriaan
*/
public interface Manager {
/**
* Gets a new job
*
* #return
*/
public Job getNewJob();
/**
* Indicates the job is completed
*
* #param job
*/
public void jobCompleted(Job job);
}
Job
import java.util.HashSet;
import java.util.Set;
/**
* A Job is a unit of work defined by a String (the description). During execution the
* job can obtain results and new job descriptions.
*
* #author Adriaan
*/
public abstract class Job {
private String description;
private Set<String> results = new HashSet<String>();
private Set<String> newDescriptions = new HashSet<String>();
/**
* Sets the job description
*
* #param description
* #return this for chaining
*/
public Job setDescription(String description) {
this.description = description;
return this;
}
/**
* Executes the job
*/
public abstract void execute();
/**
* Gets the results obtained
*
* #return
*/
public Set<String> getResults() {
return results;
}
/**
* Gets the now job descriptions obtained
*
* #return
*/
public Set<String> getNewDescriptions() {
return newDescriptions;
}
/**
* Gets the job description
*
* #return
*/
public String getDescription() {
return description;
}
/**
* Allows the implementation to add an obtained result
*
* #param result
*/
void addResult(String result) {
results.add(result);
}
/**
* Allows the implementation to add an obtained description
*
* #param result
*/
void addNewDescription(String newDescription) {
newDescriptions.add(newDescription);
}
}
A Job which crawls a page for email addresses:
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A Job which crawls HTTP or HTTPS URL's for email adresses, collecting new
* URL's to crawl along the way.
*
* #author Adriaan
*/
public class EmailAddressCrawlJob extends Job {
#Override
public void execute() {
try {
URL url = new URL(getDescription());
if (url != null) {
String text = readText(url);
extractNewDescriptions(text, url);
extractResults(text);
}
} catch (MalformedURLException e) {
System.err.println("Bad url " + getDescription());
}
}
private String readText(URL url) {
URLConnection connection;
try {
connection = url.openConnection();
InputStream input = connection.getInputStream();
byte[] buffer = new byte[1000];
int num = input.read(buffer);
if (num > 0) {
StringBuilder builder = new StringBuilder();
builder.append(new String(buffer, 0, num));
while (num != -1) {
num = input.read(buffer);
if (num != -1) {
builder.append(new String(buffer, 0, num));
}
}
return builder.toString();
}
} catch (IOException e) {
//System.err.println("Could not read from " + url);
}
return "";
}
private void extractNewDescriptions(String text, URL url) {
// URL extracting code from Sun example
String lowerCaseContent = text.toLowerCase();
int index = 0;
while ((index = lowerCaseContent.indexOf("<a", index)) != -1) {
if ((index = lowerCaseContent.indexOf("href", index)) == -1) {
break;
}
if ((index = lowerCaseContent.indexOf("=", index)) == -1) {
break;
}
index++;
String remaining = text.substring(index);
StringTokenizer st = new StringTokenizer(remaining, "\t\n\r\">#");
String strLink = st.nextToken();
if (strLink.startsWith("javascript:")) {
continue;
}
URL urlLink;
try {
urlLink = new URL(url, strLink);
strLink = urlLink.toString();
} catch (MalformedURLException e) {
// System.err.println("Could not create url: " + target
// + " + " + strLink);
continue;
}
// only look at http links
String protocol = urlLink.getProtocol();
if (protocol.compareTo("http") != 0
&& protocol.compareTo("https") != 0) {
// System.err.println("Ignoring: " + protocol
// + " protocol in " + urlLink);
continue;
}
addNewDescription(urlLink.toString());
}
}
private void extractResults(String text) {
Pattern p = Pattern
.compile("([\\w\\-]([\\.\\w])+[\\w]+#([\\w\\-]+\\.)+[A-Za-z]{2,4})");
Matcher m = p.matcher(text);
while (m.find()) {
addResult(m.group(1));
}
}
}
I know this answer is a bit verbose, but I thought OP might be best helped with a working example and I happened to have made one not so long ago.
A very basic java program that will give the abstract idea of the Multi Threading..
public class MyThread extends Thread {
String word;
public MyThread(String rm){
word = rm;
}
public void run(){
try {
for(;;){
System.out.println(word);
Thread.sleep(1000);
}
} catch(InterruptedException e) {
System.out.println("sleep interrupted");
}
}
public static void main(String[] args) {
Thread t1=new MyThread("First Thread");
Thread t2=new MyThread("Second Thread");
t1.start();
t2.start();
}
}
And the Output will be..
First Thread
Second Thread
First Thread
Second Thread
First Thread
Go with this PPT it will help you with the basics..
Here