Thread
My problem is to have the two class below to run in 2 parallel threads. I have been advised to use a work thread but its some hard stuff for a java beginner like me. Plze can I get a hand?
class 1
publicclass ttteer{
private SimpleRead;
public ttteer(){
Read =new SimpleRead(this);
}
publicvoid setData(String s){
//the code here set the text in GUI
}
publicstaticvoid main(String[] args)throws IOException{
SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
try{
ttteer application =new ttteer();
application.getJFrame().setVisible(true);
}catch(IOException w){
}
}
});
}
}
class 2
publicclass SimpleRead{
public SimpleRead(){
//the code here opens the serial port
}
publicvoid serialEvent(SerialPortEvent event){
//here i read the data from the serial port whenever there is a serial port event
}
publicvoid writeToport(){
//here I write to the port
}
}
[2690 byte] By [
musiigea] at [2007-11-27 10:50:46]

# 1
And the problem is...that you dont seem to start up the THREAD itself...I fell for that one too, way back...
What you need is something like this:
[b][i]Athing thing = new Athing();
Thread t = new Thread(thing);
t.start;[/i][/b]
the trick is to instance your Class (in the example : thing) and make a new Thread (here t) and use the name of your Class (that has to run as a Thread) as parameter.
I hope this helps...
Thor
P.S. of course you will not forget ti import the Thread...
# 2
error
Mistake posted and deleted...
Message was edited by:
petes1234
# 3
> My problem is to have the two class below to run in 2
> parallel threads.
Here's the basic structure of the code.
public class Musiige{
// this is thread #0, main thread of your app
public static void main(String[] args){
TttEer te = new TttEer();
new Thread(te).start(); // start thread #2
}
static class TttEer implements Runnable{
SimpleRead read;
public TttEer(){
read = new SimpleRead(this);
new Thread(read).start(); // start thread #1
}
public void run(){
// main task of the TttEer object
// ...
// ...
// ...
}
// other stuff
// ...
// ...
// ...
}
static class SimpleRead implements Runnable{
TttEer ter;
public SimpleRead(TttEer te){
ter = te;
}
public void run(){
// main task of the SimpleRead object
// ...
// ...
// ...
}
// other stuff
// ...
// ...
}
}
hiwaa at 2007-7-29 11:27:18 >

# 4
I found the tutorial @
http://java.sun.com/docs/books/tutorial/essential/concurrency/procthread.html
useful. Especially the guraded-block example for synchronizing serial IO.
# 5
My problem is that the main method in my simpleRead class has to be called public void serialEvent(SerialPortEvent event)
and therefore I cannot call it run. How can I solve this problem?
# 6
public class Main {
private final JFrame frame;
private JTextField textField;
public Main() {
frame = new JFrame();
textField = new JTextField();
frame.add(textField);
}
protected Window getJFrame() {
return frame;
}
static Main main;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
main = new Main();
main.getJFrame().setVisible(true);
}
});
SimpleRead reader = new SimpleRead(main);
new Thread(reader).start();
}
public void setData(String s){
textField.setText(s);
}
}
class SimpleRead implements Runnable implements SerialHandlerListener {
private boolean alive = true;
private List<String> actionQueue = new ArrayList<String>();
final Main main;
public SimpleRead(Main main) {
this.main = main;
}
@Override
public void run() {
SerialHandler serialHandler = new SerialHandler();
serialHandler.addSerialHandlerListener(this);
Object actionsRecevied[] = null;
while(alive) {
synchronized(this) {
while(actionQueue.isEmpty && alive) {
this.wait(20000);
}
actionsRecevied = actionQueue.toArray();
actionQueue.clear();
}
if (actionsRecevied!=null && actionsRecevied.length>0) {
StringBuilder allActions = new StringBuilder();
for(Object action : actionsRecevied) {
String actionText = (String) action;
writeToPort(actionText+" - my response");
allActions.append(actionText).append(" ");
}
main.setData("received next actions:"+allActions.toString());
}
}
}
public synchronized serialEvent(SerialPortEvent event){
if (event.text.equals("STOP")) {
alive = false;
} else {
actionQueue.add(event.text);
}
actionQueue.notifyAll();
}
public void writeToport(String responseText){
serialHandler.writeText(responseText);
}
}
# 7
Thanks alot man but I do not really understand this method. In my own code, it is supposed to send an array of bytes. Can I just modify it?
>
> public void writeToport(String responseText){
> serialHandler.writeText(responseText);
> }
> }
>
# 8
I do get this error "SerialHandlerListener cannot be resolved to a type" how can I solve it? plze
# 9
I have got SerialHandlerListener on the net and it needs many classes which also need many other classes. Does anyone know a package I can just import instead?
# 10
The SerialHandlerListener and the SerialHandler are not implemented types in my example. They do not already exist (you could programm them). I don't know the exact implementation for serial connections. Searching on google I found that Sun already provides you with a ready to use API. http://java.sun.com/products/javacomm/ There are some simple examples in the online documentation these should help you getting started.