Exception using Threads

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Exception using Threads

Postby esmetaman » Wed Sep 12, 2007 8:26 pm

Hi,

I am developing a Class to control communications between 2 NXT bricks using Threads, but I have problems with my implementation.

The main Class, create 2 instance:

+ BT Listener
+ NXT Menu

I use to test the listener, the example BTConnectTest, and I notice that I receive in Sender data then BT Listener Thread runs. When that Thread finish to execute. I see a Java Exception Class 5, method: 1

Help me.

Main:


public class NXTCommTest {

/**
* @param args
*/
public static void main(String[] args) {

try{
NXTBotMenu NBM = new NXTBotMenu();
NXTCommsListener NCL = new NXTCommsListener(NBM);

NBM.start();
NCL.start();
}catch(Exception e){

}
}

}

Thread listener:

import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;

public class NXTCommsListener extends Thread{

private String MESSAGE_AWAITING = "Awaiting";
private String MESSAGE_CONNECTED = "Connected";

//private Boolean NXT_CONNECTED = false;

private Thread _MENU;

private BTConnection btc;

public NXTCommsListener(Thread MENU)throws Exception{
_MENU = MENU;
}

private void showMessage(String message){
LCD.clear();
LCD.drawString(message,0,0);
LCD.refresh();
}

public void run()
{
while(true)
{
try
{
showMessage(MESSAGE_AWAITING);
btc = Bluetooth.waitForConnection();

showMessage(MESSAGE_CONNECTED);

_MENU.interrupt();
showMessage("Running LISTENER");

InputStream is = btc.openInputStream();
OutputStream os = btc.openOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);

for(int i=0;i<100;i++) {
int ii = dis.readInt();
LCD.drawInt(ii,3,0,1);
LCD.refresh();
dos.writeInt(-ii);
dos.flush();
}

try {
dis.close();
dos.close();
btc.close();
} catch (IOException ioe){
//
}
}
catch(Exception ex)
{
//
}
}
}
}

Menu Thread:

import lejos.nxt.*;

public class NXTBotMenu extends Thread{

NXTBotMenu() throws Exception{

}

private void showMessage(String message){
LCD.clear();
LCD.drawString(message,0,0);
LCD.refresh();
}

public void run()
{
while(true)
{
try
{
showMessage("NXTMenu");
}
catch(Exception e)
{}
}
}

}

Thanks
User avatar
esmetaman
Advanced Member
 
Posts: 238
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby esmetaman » Wed Sep 12, 2007 9:55 pm

Hi, I have updated Classes, and now is easier than previous version to see the exception:

MAIN:

import lejos.nxt.*;

public class NXTCommTest {

/**
* @param args
*/
public static void main(String[] args) {

try{
NXTBotMenu NBM = new NXTBotMenu();
NXTCommsListener NCL = new NXTCommsListener();
NBM.setOpponents(NCL);
NCL.setOpponents(NBM);
NBM.start();
NCL.start();
}catch(Exception e){

}
}

}

MENU

import lejos.nxt.*;

public class NXTBotMenu extends Thread{

private Thread _LISTENER;

NXTBotMenu() throws Exception{

}

private void showMessage(String message){
//LCD.clear();
LCD.drawString(message,0,0);
LCD.refresh();
}

public void setOpponents(Thread th1)
{
_LISTENER = th1;
}

public void run()
{
while(true)
{
showMessage("NXTMenu");
try{
//_LISTENER.interrupt();
//showMessage("NXTMenu");
//}catch(InterruptedException e){

//}
}catch(Exception e){

}
}
}

}

LISTENER

import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;

public class NXTCommsListener extends Thread{

private String MESSAGE_AWAITING = "Awaiting";
private String MESSAGE_CONNECTED = "Connected";

//private Boolean NXT_CONNECTED = false;

private Thread _MENU;

private BTConnection btc;

public NXTCommsListener()throws Exception{

}

public void setOpponents(Thread th1)
{
_MENU = th1;
//opponent2 = th2;
}

private void showMessage(String message){
//.clear();
LCD.drawString(message,0,1);
LCD.refresh();
}

public void run()
{
while(true)
{
try
{
//showMessage(MESSAGE_AWAITING);
btc = Bluetooth.waitForConnection();

//showMessage(MESSAGE_CONNECTED);


_MENU.interrupt();
showMessage("Running LISTENER");

InputStream is = btc.openInputStream();
OutputStream os = btc.openOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);

try {
for(int i=0;i<100;i++) {
int ii = dis.readInt();
LCD.drawInt(ii,3,0,2);
LCD.refresh();
dos.writeInt(-ii);
dos.flush();
}
} catch (IOException ioe) {

}
try {
dis.close();
dos.close();
btc.close();
} catch (IOException ioe){
//
}
}
{
//
}
//catch(InterruptedException ex)
//{
//
//}
//_MENU.start();
}
}
}

test this example with BTConnectTest. Listener Thread receive and send data to senderBot, but in all cases, I have a exception class 5. I dont know how to solve this problem
User avatar
esmetaman
Advanced Member
 
Posts: 238
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Re: Exception using Threads

Postby Shawn » Thu Sep 13, 2007 8:45 am

HI,

I am new...but...um

esmetaman wrote:Hi,

Code: Select all
    public void run()
    {
        while(true)
        {
            try
            {
              showMessage(MESSAGE_AWAITING);
                btc = Bluetooth.waitForConnection();
               
                showMessage(MESSAGE_CONNECTED);

             _MENU.interrupt();
                showMessage("Running LISTENER");
                           
             InputStream is = btc.openInputStream();
             OutputStream os = btc.openOutputStream();
             DataInputStream dis = new DataInputStream(is);
             DataOutputStream dos = new DataOutputStream(os);
             
             for(int i=0;i<100;i++) {
                int ii = dis.readInt();
                LCD.drawInt(ii,3,0,1);
                LCD.refresh();
                dos.writeInt(-ii);
                dos.flush();
             }

              try {
                 dis.close();
                 dos.close();
                 btc.close();
              } catch (IOException ioe){
   

don't you need to close is and os --is.close() os.close();

Not that that is what the main problem is but if you keep opening Streams without closing them -- especially if this has no garabage collection (I think I read that). Proabably should be done in opposite order IIRC ... dis.close();is.close();
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Re: Exception using Threads

Postby Shawn » Thu Sep 13, 2007 11:07 am

Shawn wrote:HI,

don't you need to close is and os --is.close() os.close();


Um I see a lot of the examples don't do that. Hmmn, I learned it was neccesary but...

To the point though, you said you are getting an OUTOFMEMORYERROR 5.

Holy Objects LegoMan, you are in a while loop in your run method creating one object after another.

At a minimum you could try:
Code: Select all

InputStream is;
OutputStream os;
DataInputStream dis;
DataOutputStream dos;

 while(true)
{
try
{
showMessage(MESSAGE_AWAITING);
btc = Bluetooth.waitForConnection();

showMessage(MESSAGE_CONNECTED);

_MENU.interrupt();
showMessage("Running LISTENER");

 is = btc.openInputStream();
 os = btc.openOutputStream();
 dis = new DataInputStream(is);
dos = new DataOutputStream(os);
to try and reuse your objects but I still feel like creating objects in a running loop will cause trouble especially as we don't have garbage collection.
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Postby esmetaman » Thu Sep 13, 2007 8:34 pm

Hi Shawn,

At the end, I solved my problem and you helped me. The cause of OUTOFMEMORY is due to LCD.drawString() method. In previous release, I detected a problem because If you use LCD.drawString() in a Loop, you will get the Famous Message OUTOFMEMORY.

Thanks. When I finish my work, I will Share my work with everyone.

Regards.

Juan Antonio Breña Moral
www.juanantonio.info
User avatar
esmetaman
Advanced Member
 
Posts: 238
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests

more stuff