SirLancillotto
SirLancillotto

Reputation: 49

Java Task stop after loop

in my project I have this code.

    Task<Void> task = new Task<Void>() {
    @Override
    protected Void call()  throws Exception {
        do {
            ...
            [do something]
            ...
        } while (true);
        [do other]
        return null;
    }
};
new Thread(task).start();

The loop working fine, but the istruction out of loop [do other] are not executed. Can you help me to understand?

Thanks.

UPDATE The "while(true)" is only an example. The loop working until the condition is true! The real command is "while(x < y)" and working fine!

This code WORK

                Callable<Void> task = new Callable<Void>() {
              @Override
              public  Void call()  throws Exception {

                    do {
                        rowBin = new ASN1BinaryModel("000A" , "A0", "80", "54", "31", "0F", "0B", "A0", "80", "54", "31", "0F", "0B", "A0", "80", "54", "31");
                        asn1BinaryTable.getItems().add(rowBin);

                        countTotByte = countTotByte + 16;

                        System.out.println("HEX: " + countTotByte);

                    } while (countTotByte <= len);
                    System.out.println("END LOOP");
                  return null;

This code NOT WORK

                Callable<Void> task = new Callable<Void>() {
              @Override
              public  Void call()  throws Exception {

                    do {
                        tmpAddress = Integer.toHexString(countTotByte).toUpperCase();
                        rowBin = new ASN1BinaryModel(String.format("%4s", tmpAddress).replace(" ", "0") , String.format("%02X", data[countTotByte]), 
                                String.format("%02X", data[countTotByte+1]), String.format("%02X", data[countTotByte+2]), String.format("%02X", data[countTotByte+3]), 
                                String.format("%02X", data[countTotByte+4]), String.format("%02X", data[countTotByte+5]), String.format("%02X", data[countTotByte+6]),
                                String.format("%02X", data[countTotByte+7]), String.format("%02X", data[countTotByte+8]), String.format("%02X", data[countTotByte+9]), 
                                String.format("%02X", data[countTotByte+10]), String.format("%02X", data[countTotByte+11]), String.format("%02X", data[countTotByte+12]), 
                                String.format("%02X", data[countTotByte+13]), String.format("%02X", data[countTotByte+14]), String.format("%02X", data[countTotByte+15]));

                        asn1BinaryTable.getItems().add(rowBin);

                        countTotByte = countTotByte + 16;

                        System.out.println("HEX: " + countTotByte);

                    } while (countTotByte <= len);
                    System.out.println("END LOOP");
                  return null;

Upvotes: 0

Views: 201

Answers (3)

SirLancillotto
SirLancillotto

Reputation: 49

Ok, I am idiot... sorry The problem is in byte data[] array.

I read a file and insert bytes in data[size] where [size] is file lenght. After i get 16 byte to time and adding row to TableView. The problem si the last X bytes, because is not sure that they are 16! In this way the command:

String.format("%02X", data[countTotByte+n])

fails when finds the first byte[] "empty".

I am sorry again...

Upvotes: 0

Rob Audenaerde
Rob Audenaerde

Reputation: 20039

do { ... } while (true) keeps looping.

Try using a variable instead of true and set this variable to false. The loop will end and [do other] will get executed.

I'm not sure about your Task, but changed to Callable and ExecutorService:

Here is an example:

public static void main(String[] args) {
    Callable<Void> task = new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            int i = 0;
            do {
                i++;
                System.out.println(i);
            } while (i < 10);
            System.out.println("end");
            return null;
        }
    };

     ExecutorService executor = Executors.newFixedThreadPool(10);
     executor.submit(task);

}

Output:

1
2
3
4
5
6
7
8
9
10
end

Upvotes: 1

DevJava
DevJava

Reputation: 396

it looks like you have an infinite loop. make sur that x >= y in some how after some executions of the loop in order to get out of it.

Upvotes: 1

Related Questions