• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in while loop  RSS feed

 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I am writing a multi-threaded program in which thread A which is running in background & getting resources from network which the thread B is going to use.

Now when i am holding thread B, which is to be keep running until thread A completes its working by using while loop like this-

while(boolean){
continue;
}

It is not working, while loop does not execute repeatedly & control comes out of while loop even if boolean is true.

However if i write some statement before continue like this

while(boolean){
System.out.println("stopped");
continue;
}

it is working.
Can someone please tell me why first loop is not working.


Regards,
Anuj
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to a more appropriate forum
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i got it

Actually it was a j2me application & i was obfuscating it before compling. And obfuscation process removed while loop because it was doing nothing thats why it was not working.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would wonder the need for that control structure. You would do better to run code that would not take processor time while blocking the Thread. For example:
1) Using threadA.join();
2) Using wait()/notify()
3) Using java.util.concurrent.Lock/Condition (if they are available on J2ME, not sure if they are).

The point is that the while (boolean) {} loop is processor intensive. It must continue to run, taking processor time away from the other tasks you really want to run. This makes your entire application run slower. These other methods would put the Thread into a waiting state which does not use any processor time until they are woken up from an external source (such as the other Thread).
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for suggestion ..

Actually I am not using join because interrupt method is not available in cldc 1.0 and i want to make my application cldc 1.0 compatable.

I have not thought about using wait notify .I will try it now. Thanks for your pointing out my mistake.
I never thought about that the while (boolean) {} loop will take processor time.

Regards,
Anuj
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!