Midi device connection and latency - java

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.

Related

Running a .java file and instantiating the class contained in it give me different results

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);
}
}
}

jSSC Not Sending Data to Arduino

I have a problem with writing function of jSSC. My Arduino Uno board seems not getting data from my Java program.
I have a stepper motor controlled by Arduino Uno board. I made a simple program that has 2 buttons. One is for CW rotation and the other is CCW rotation. CW button sends 'H' char and CCW button sends 'L' char. Now I have:
I checked from Arduino IDE serial console my Arduino program works correct. When I send 'H' the motor turns CW and with 'L' the motor turns CCW.
I made a program in Processing with two buttons sending 'H' and 'L'. It worked.
I made a Java program with JSSC with two buttons sending 'H' and 'L'. IT FAILED.
When I push one of the buttons in my program I see "L" light on the board blinks 3-4 times but nothing happens.
I tried getting data from my board with JSSC and it worked. It seems the problem is in writing function.
I checked with another Arduino Uno board but the result is the same.
My Java program uses serialPort.writeByte((byte)'H'); and serialPort.writeByte((byte)'L');
Any ideas?
Did you try setting the parameters for flow control. Since while writing to the interface, it requires a permission.
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE,false,true);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0,RTSEnable,DTSEnable);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
Can it be a reset-related problem? Maybe your java function uses the DTR pin, which is connected to the RESET pin; so when you try to send data instead you are just resetting the board.
If you want to test you can make another led blink at startup or send something through the serial interface at setup. If you get that feedback try looking at the way to disable DTR ;)
You can refactor Processing's Serial class (which works pretty well) to avoid PApplet if you don't plan to use it in your java project:
/*
PSerial - class for serial port goodness
Part of the Processing project - http://processing.org
Copyright (c) 2004-05 Ben Fry & Casey Reas
Reworked by Gottfried Haider as part of GSOC 2013
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
import java.lang.reflect.Method;
import java.util.Map;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
import jssc.SerialPortList;
public class Serial implements SerialPortEventListener {
Object parent;
public SerialPort port;
Method serialAvailableMethod;
Method serialEventMethod;
byte[] buffer = new byte[32768];
int inBuffer = 0;
int readOffset = 0;
int bufferUntilSize = 1;
byte bufferUntilByte = 0;
volatile boolean invokeSerialAvailable = false;
// Things we are currently not exposing:
// * hardware flow control
// * state of the RING, RLSD line
// * sending breaks
public Serial(Object parent) {
this(parent, "COM1", 9600, 'N', 8, 1);
}
public Serial(Object parent, int baudRate) {
this(parent, "COM1", baudRate, 'N', 8, 1);
}
public Serial(Object parent, String portName) {
this(parent, portName, 9600, 'N', 8, 1);
}
public Serial(Object parent, String portName, int baudRate) {
this(parent, portName, baudRate, 'N', 8, 1);
}
public Serial(Object parent, String portName, int baudRate, char parity, int dataBits, float stopBits) {
this.parent = parent;
//parent.registerMethod("dispose", this);
//parent.registerMethod("pre", this);
// setup parity
if (parity == 'O') {
parity = SerialPort.PARITY_ODD;
} else if (parity == 'E') {
parity = SerialPort.PARITY_EVEN;
} else if (parity == 'M') {
parity = SerialPort.PARITY_MARK;
} else if (parity == 'S') {
parity = SerialPort.PARITY_SPACE;
} else {
parity = SerialPort.PARITY_NONE;
}
// setup stop bits
int stopBitsIdx = SerialPort.STOPBITS_1;
if (stopBits == 1.5f) {
stopBitsIdx = SerialPort.STOPBITS_1_5;
} else if (stopBits == 2) {
stopBitsIdx = SerialPort.STOPBITS_2;
}
port = new SerialPort(portName);
try {
// the native open() call is not using O_NONBLOCK, so this might block for certain operations (see write())
port.openPort();
port.setParams(baudRate, dataBits, stopBitsIdx, parity);
// we could register more events here
port.addEventListener(this, SerialPort.MASK_RXCHAR);
} catch (SerialPortException e) {
// this used to be a RuntimeException before, so stick with it
throw new RuntimeException("Error opening serial port " + e.getPortName() + ": " + e.getExceptionType());
}
serialEventMethod = findCallback("serialEvent");
serialAvailableMethod = findCallback("serialAvailable");
}
private Method findCallback(final String name) {
try {
return parent.getClass().getMethod(name, this.getClass());
} catch (Exception e) {
}
// Permit callback(Object) as alternative to callback(Serial).
try {
return parent.getClass().getMethod(name, Object.class);
} catch (Exception e) {
}
return null;
}
public void dispose() {
stop();
}
public void pre() {
if (serialAvailableMethod != null && invokeSerialAvailable) {
invokeSerialAvailable = false;
try {
serialAvailableMethod.invoke(parent, this);
} catch (Exception e) {
System.err.println("Error, disabling serialAvailable() for "+port.getPortName());
System.err.println(e.getLocalizedMessage());
serialAvailableMethod = null;
}
}
}
public int available() {
return (inBuffer-readOffset);
}
public void buffer(int size) {
bufferUntilSize = size;
}
public void bufferUntil(int inByte) {
bufferUntilSize = 0;
bufferUntilByte = (byte)inByte;
}
public void clear() {
synchronized (buffer) {
inBuffer = 0;
readOffset = 0;
}
}
public boolean getCTS() {
try {
return port.isCTS();
} catch (SerialPortException e) {
throw new RuntimeException("Error reading the CTS line: " + e.getExceptionType());
}
}
public boolean getDSR() {
try {
return port.isDSR();
} catch (SerialPortException e) {
throw new RuntimeException("Error reading the DSR line: " + e.getExceptionType());
}
}
public static Map<String, String> getProperties(String portName) {
return SerialPortList.getPortProperties(portName);
}
public int last() {
if (inBuffer == readOffset) {
return -1;
}
synchronized (buffer) {
int ret = buffer[inBuffer-1] & 0xFF;
inBuffer = 0;
readOffset = 0;
return ret;
}
}
public char lastChar() {
return (char)last();
}
public static String[] list() {
// returns list sorted alphabetically, thus cu.* comes before tty.*
// this was different with RXTX
return SerialPortList.getPortNames();
}
public int read() {
if (inBuffer == readOffset) {
return -1;
}
synchronized (buffer) {
int ret = buffer[readOffset++] & 0xFF;
if (inBuffer == readOffset) {
inBuffer = 0;
readOffset = 0;
}
return ret;
}
}
public byte[] readBytes() {
if (inBuffer == readOffset) {
return null;
}
synchronized (buffer) {
byte[] ret = new byte[inBuffer-readOffset];
System.arraycopy(buffer, readOffset, ret, 0, ret.length);
inBuffer = 0;
readOffset = 0;
return ret;
}
}
public int readBytes(byte[] dest) {
if (inBuffer == readOffset) {
return 0;
}
synchronized (buffer) {
int toCopy = inBuffer-readOffset;
if (dest.length < toCopy) {
toCopy = dest.length;
}
System.arraycopy(buffer, readOffset, dest, 0, toCopy);
readOffset += toCopy;
if (inBuffer == readOffset) {
inBuffer = 0;
readOffset = 0;
}
return toCopy;
}
}
public byte[] readBytesUntil(int inByte) {
if (inBuffer == readOffset) {
return null;
}
synchronized (buffer) {
// look for needle in buffer
int found = -1;
for (int i=readOffset; i < inBuffer; i++) {
if (buffer[i] == (byte)inByte) {
found = i;
break;
}
}
if (found == -1) {
return null;
}
int toCopy = found-readOffset+1;
byte[] dest = new byte[toCopy];
System.arraycopy(buffer, readOffset, dest, 0, toCopy);
readOffset += toCopy;
if (inBuffer == readOffset) {
inBuffer = 0;
readOffset = 0;
}
return dest;
}
}
public int readBytesUntil(int inByte, byte[] dest) {
if (inBuffer == readOffset) {
return 0;
}
synchronized (buffer) {
// look for needle in buffer
int found = -1;
for (int i=readOffset; i < inBuffer; i++) {
if (buffer[i] == (byte)inByte) {
found = i;
break;
}
}
if (found == -1) {
return 0;
}
// check if bytes to copy fit in dest
int toCopy = found-readOffset+1;
if (dest.length < toCopy) {
System.err.println( "The buffer passed to readBytesUntil() is to small " +
"to contain " + toCopy + " bytes up to and including " +
"char " + (byte)inByte);
return -1;
}
System.arraycopy(buffer, readOffset, dest, 0, toCopy);
readOffset += toCopy;
if (inBuffer == readOffset) {
inBuffer = 0;
readOffset = 0;
}
return toCopy;
}
}
public char readChar() {
return (char) read();
}
public String readString() {
if (inBuffer == readOffset) {
return null;
}
return new String(readBytes());
}
public String readStringUntil(int inByte) {
byte temp[] = readBytesUntil(inByte);
if (temp == null) {
return null;
} else {
return new String(temp);
}
}
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() == SerialPortEvent.RXCHAR) {
int toRead;
try {
while (0 < (toRead = port.getInputBufferBytesCount())) {
// this method can be called from the context of another thread
synchronized (buffer) {
// read one byte at a time if the sketch is using serialEvent
if (serialEventMethod != null) {
toRead = 1;
}
// enlarge buffer if necessary
if (buffer.length < inBuffer+toRead) {
byte temp[] = new byte[buffer.length<<1];
System.arraycopy(buffer, 0, temp, 0, inBuffer);
buffer = temp;
}
// read an array of bytes and copy it into our buffer
byte[] read = port.readBytes(toRead);
System.arraycopy(read, 0, buffer, inBuffer, read.length);
inBuffer += read.length;
}
if (serialEventMethod != null) {
if ((0 < bufferUntilSize && bufferUntilSize <= inBuffer-readOffset) ||
(0 == bufferUntilSize && bufferUntilByte == buffer[inBuffer-1])) {
try {
// serialEvent() is invoked in the context of the current (serial) thread
// which means that serialization and atomic variables need to be used to
// guarantee reliable operation (and better not draw() etc..)
// serialAvailable() does not provide any real benefits over using
// available() and read() inside draw - but this function has no
// thread-safety issues since it's being invoked during pre in the context
// of the Processing applet
serialEventMethod.invoke(parent, this);
} catch (Exception e) {
System.err.println("Error, disabling serialEvent() for "+port.getPortName());
System.err.println(e.getLocalizedMessage());
serialEventMethod = null;
}
}
}
invokeSerialAvailable = true;
}
} catch (SerialPortException e) {
throw new RuntimeException("Error reading from serial port " + e.getPortName() + ": " + e.getExceptionType());
}
}
}
public void setDTR(boolean state) {
// there is no way to influence the behavior of the DTR line when opening the serial port
// this means that at least on Linux and OS X, Arduino devices are always reset
try {
port.setDTR(state);
} catch (SerialPortException e) {
throw new RuntimeException("Error setting the DTR line: " + e.getExceptionType());
}
}
public void setRTS(boolean state) {
try {
port.setRTS(state);
} catch (SerialPortException e) {
throw new RuntimeException("Error setting the RTS line: " + e.getExceptionType());
}
}
public void stop() {
try {
port.closePort();
} catch (SerialPortException e) {
// ignored
}
inBuffer = 0;
readOffset = 0;
}
public void write(byte[] src) {
try {
// this might block if the serial device is not yet ready (esp. tty devices under OS X)
port.writeBytes(src);
// we used to call flush() here
} catch (SerialPortException e) {
throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType());
}
}
public void write(int src) {
try {
port.writeInt(src);
} catch (SerialPortException e) {
throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType());
}
}
public void write(String src) {
try {
port.writeString(src);
} catch (SerialPortException e) {
throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType());
}
}
}
Here's an example class using the above:
public class SerialTest {
public static void main(String[] args) {
try{
Serial serial = new Serial(new SerialTest(),"/dev/tty.usbmodemfd121",115200);
serial.write("H");
serial.write("L");//can also try serial.write((int)'L');
}catch(Exception e){
System.err.println("serial not connected!");
}
}
public void serialAvailable(Serial s){
System.out.println(s.toString());
}
public void serialEvent(Serial s){
System.out.print("from serial:");
System.out.println(s.read());
}
}
Be sure to change the port and baud rate to what your Arduino Uno is using.
In Processing's serial library folder you'll also find the JNI native libraries.
For example on Windows:
C:\Program Files\processing-2.1.2\modes\java\libraries\serial\library
Where the dll would reside in the windows32 and windows64 folders, depending on what you plan to use.
The Serial class above is pretty much the same as C:\Program Files\processing-2.1.2\modes\java\libraries\serial\src\processing\serial/Serial.java replacing PApplet with Object.
Also, here is the Processing Serial library reference

Why is there no output in my file after using Writer? [closed]

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.

Switching between client threads in a Java server

I am working on a project trying to make several people be able to control a robot arm. For this they have to connect to a Java server that then sends the commands to a robot screen for video conferencing.
I am trying to have a thread for each client and then I want to be able to switch between the different clients based on sound, because I want the speaker to be able to control the robot.
The clients all provide positional data and the level of sound taken by the kinect, and sent to the server in the form of a string.
I am having problems with performing the switch. Currently they seem to be switching back and forth and it makes the robot go haywire.
Is there a good way of comparing the threads to each other, find the appropriate one, switch to that, all the while checking the other threads to see if or when they become the most appropriate one? While also checking in case other clients try to connect to the server?
Thank you for your help.
I also include my code in case you want to look through it and get a better idea.
This is the server class:
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Hashtable;
public class MultiThreadedServer implements Runnable {
protected int serverPort = 8888;
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
protected Thread runningThread = null;
protected Thread clientThread = null;
protected Thread threadThread = null;
private Hashtable<Long, WorkerRunnable> Users = new Hashtable<Long, WorkerRunnable>();
private ArrayList<Thread> ClientThreads = new ArrayList<Thread>();
private WorkerRunnable client = null;
private ThreadHandler threadHandler = null;
private int sound_max = 0;
private boolean once = true;
public MultiThreadedServer (int port) {
this.serverPort = port;
}
public void run() {
synchronized(this) {
this.runningThread = Thread.currentThread();
}
openServerSocket();
threadHandler = new ThreadHandler();
while( !isStopped() ) {
Socket clientSocket = null;
try {
System.out.println(InetAddress.getLocalHost());
clientSocket = this.serverSocket.accept(); // Connect to clients
} catch (SocketTimeoutException e) {
} catch (IOException e) {
if( isStopped() ) {
System.out.println("Server Stopped");
return;
}
throw new RuntimeException("Error accepting client connection", e);
}
client = new WorkerRunnable(clientSocket, "Multithreaded Server");//Class does client work
clientThread = new Thread(client); // Make a thread for each client
clientThread.start(); // start thread
threadHandler.setUp(client, clientThread); // Set up the thread handler
if ( once == true) { // make sure the threadHandler thread is only created once
threadThread = new Thread(threadHandler);
threadThread.start();
once = false;
}
}
System.out.println("Server Stopped");
}
/**
* Check if the socket is stopped
* #return true if the socket is stopped
*/
private synchronized boolean isStopped() {
return this.isStopped;
}
/**
* Stop and close the socket
*/
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
/**
* Open server socket
*/
private void openServerSocket() {
try {
this.serverSocket = new ServerSocket(this.serverPort);
} catch (IOException e) {
throw new RuntimeException("Cannot open port 8888", e);
}
}
}
This is the Worker class, that handles the data from the clients:
import gnu.io.NoSuchPortException;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class WorkerRunnable implements Runnable {
protected Socket clientSocket = null;
protected String serverText = null;
private BufferedReader inFromClient;
private DataOutputStream outToClient;
private int[] currentPos = new int[6];
private boolean connected = false;
static TwoWaySerialComm serialCom = null;
static MultiServoState mState;
static int sound_average;
int[] degrees = new int[7];
int count = 0;
public WorkerRunnable(Socket clientSocket, String serverText) {
this.clientSocket = clientSocket;
this.serverText = serverText;
initCurrentPos();
if (serialCom == null) {
serialCom = new TwoWaySerialComm();
}
try {
if (!serialCom.isConnected("COM5")) {
try {
serialCom.connect("COM5");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mState = new MultiServoState(serialCom);
}
} catch (NoSuchPortException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
try {
work();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
}
public void work() throws InterruptedException {
try {
InputStream input = clientSocket.getInputStream();
OutputStream output = clientSocket.getOutputStream();
inFromClient = new BufferedReader(new InputStreamReader(input));
outToClient = new DataOutputStream(output);
long time = System.currentTimeMillis();
updateData();
String message = null;
long endTime = System.currentTimeMillis() + 2000;
while ((message = (String) inFromClient.readLine()) != null) {
System.out.println("Message Received: " + message);
parse(message);
sound_average = degrees[6];
//
// Send the positional data to the robot
//
mState.runServo(degrees[0], degrees[1], degrees[2],
degrees[3], degrees[4], degrees[5]);
//
// Send a response information to the client application
//
currentPos[0] = mState.getCurrentPos(0);
currentPos[1] = mState.getCurrentPos(1);
currentPos[2] = mState.getCurrentPos(2);
currentPos[3] = mState.getCurrentPos(3);
currentPos[4] = mState.getCurrentPos(4);
try {
updateData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Request processed: " + time);
} catch (IOException e) {
// report exception somewhere
e.printStackTrace();
}
}
/**
* Initiate the robot's starting position.
*/
public void initCurrentPos()
{
currentPos[0] = 100;
currentPos[1] = 100;
currentPos[2] = 100;
currentPos[3] = 100;
currentPos[4] = 100;
currentPos[5] = 0;
}
/**
* Send the data to the client
*
* #throws IOException
*/
public void updateData() throws IOException {
String sentence = Integer.toString(currentPos[0]) + ", " +
Integer.toString(currentPos[1]) + ", " +
Integer.toString(currentPos[2]) + ", " +
Integer.toString(currentPos[3]) + ", " +
Integer.toString(currentPos[4]) + "." + "\n";
outToClient.flush();
outToClient.writeBytes(sentence);
}
/**
* Get the clients sound average
* #param message
*/
public int getSoundAverage() {
return sound_average;
}
public void parse(String message) {
if (message != null) {
char c;
StringBuilder sb = new StringBuilder(4);
int j = 0;
boolean help = false;
for (int i = 0; i < message.length(); i++) {
c = message.charAt(i);
if (Character.isDigit(c)) {
sb.append(c);
help = true;
}
if (!Character.isDigit(c) && help == true) {
degrees[j] = Integer.parseInt(sb.toString());
j++;
help = false;
sb.delete(0, sb.length());
}
}
}
System.out.println("Waiting for client message...");
}
/**
* Close all connections
*/
public void close() {
if (connected) {
synchronized (this) {
connected = false;
}
if (outToClient != null) {
try {
outToClient.close();
synchronized (this) {
outToClient = null;
}
} catch (IOException e) {
// there is nothing we can do: ignore it
}
}
if (inFromClient != null) {
try {
inFromClient.close();
synchronized (this) {
inFromClient = null;
}
} catch (IOException e) {
// there is nothing we can do: ignore it
}
}
if (clientSocket != null) {
try {
clientSocket.close();
synchronized (this) {
clientSocket = null;
}
} catch (IOException e) {
// there is nothing we can do: ignore it
}
}
}
}
public void returnThread() {
return;
}
}
The final class is the thread handler where I try to compare sound levels and yield all threads except the loudest one:
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Hashtable;
import com.research.aserver.WorkerRunnable;
public class ThreadHandler implements Runnable {
protected boolean isStopped = false;
protected Thread runningThread = null;
protected Thread clientThread = null;
private Hashtable<Long, WorkerRunnable> Users = new Hashtable<Long, WorkerRunnable>();
private ArrayList<Thread> ClientThreads = new ArrayList<Thread>();
private WorkerRunnable client = null;
private int sound_max = 0;
private int index = 0;
public ThreadHandler() {
}
public void setUp(WorkerRunnable client, Thread clientThread) {
this.client = client;
this.clientThread = clientThread;
Users.put(clientThread.getId(), this.client); // Place clients in a list with its thread ID as key
ClientThreads.add(this.clientThread); // List of client threads
}
#Override
public void run() {
long endTime = System.currentTimeMillis() + 2000; // Help variable to check every 2 sec
while (!Users.isEmpty() && !ClientThreads.isEmpty()) {
for (int i = 0; i < ClientThreads.size(); i++) { // Remove clients and threads if no longer active
if (!ClientThreads.get(i).isAlive()) {
Users.remove(ClientThreads.get(i).getId());
ClientThreads.get(i).interrupt();
ClientThreads.remove(i);
}
}
if(System.currentTimeMillis() >= endTime) { // Do work every 2 sec
for (int i = 0; i < ClientThreads.size(); i++) { // Get the client with the loudest sound
if (sound_max < Users.get(ClientThreads.get(i).getId()).getSoundAverage()) {
sound_max = Users.get(ClientThreads.get(i).getId()).getSoundAverage();
index = i;
}
}
for (int i = 0; i < ClientThreads.size(); i++) { // yield all threads that are not the loudest
if (Users.get(ClientThreads.get(index).getId()) != Users.get(ClientThreads.get(i).getId())){
ClientThreads.get(i).yield();
index = 0;
}
}
endTime = System.currentTimeMillis() + 2000; // update time
}
sound_max = 0;
}
}
}
One idea might be to use a PriorityBlockingQueue and define a quality value for each input, which is then sort by quality automatically inside the list.
Using this your consumer thread can simply fetch the first one in line and process it, knowing that it is the most appropriate one, while the generator threads can simply throw all input in the Queue.

Make an existing code in Java parallel/multithread

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

Categories