Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic Switching between two are more methods  RSS feed

 
singh gaurav
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is Possible in java to switch between methods dyanamically, I have a method which create a client and this client holds a connection to a server, and server reset connection after some period of time automatically, to save this connection client, we use a hello () method periodically, to call hello method periodically we have a method inside method a infinite loop is running and calling hello () after some time periodically. By using this client we have to perform some operations declared in some other methods, for invoking other operation methods i have to stop method hello (), and perform some operations,Then again to save our client and connection to server we have to call hello (),

Class xyz{
method Create client ()
{
Return client
}
Method say hello()
{
For(infinite loop)
{
Client.hello()
}
}
Method m1(){client.some work}
Method m2(){client.somework}
.
.
.
Method mN(){client.some work}
}
Here I have to switch between method sayhello() and m1() m2() m3()……mN() dynamically and method create client() called just once
 
Phil Freihofner
Ranch Hand
Posts: 126
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may be totally misunderstanding your question, but maybe put the various methods as separate threads in some sort of ExecutorPool? Then, the JVM will do the switching.

Or, why not have the tasks that require the hello method just call it directly themselves when they need it?

Also, check out the "code" button above. It is very helpful when you display code, makes it much more readable if you put your code between code tags.
 
singh gaurav
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response, i think to implement this logic can you justify it
 
singh gaurav
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code and discussions
kindly visit to know more about my problem
 
Phil Freihofner
Ranch Hand
Posts: 126
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid I don't have the time or brain capacity right now to try and decipher the code at your previous link. But, you CAN still go back and edit your earlier posts and put [ code][ /code] tags around them to make them more readable for others who might help.

The example below is the best I have from my own code that stops a thread dynamically. Maybe you can use it as a model or as a suggestion on how to do your switching. The technique I use is to "request" the thread stop via a boolean. I do this with the boolean "playing". I place "playing == true" as a condition in any while loops or as a test in any for loops. If I want the sound-playback thread to stop, I set its "playing" boolean to false via the command myLoop.stop().



This program plays a cue over and over again. It makes two copies of the sound cue and plays them alternately, in their own threads, allowing them to overlap. (Overlaps occur if the lenMillis value is set to less than the total length of the cue in milliseconds.)

The tricky part was getting this loop-player to stop, dynamically. I do so by use of the close() method which sets a "playing" boolean to false. I put comments in the code to emphasize where this occurs.

I would also recommend checking out "Java Concurrency in Practice" by Brian Goetz. Chapter 7, in particular, talks about how to properly shut down concurrent threads.

Best of luck with your project!
 
singh gaurav
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very sorry for not using "[code]" and hope in future it will not happen,

Thanks for your kind response.
 
Rohan Dhapodkar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be i am wrong in interpreting the requirements. But this looks like some ConnectionPooling problem where your connection will be resetted if ideal for some time.
Probably you can have some pool manager which holds ideal connection with timer thread which will run periodically and executes helo() method on all ideal connections. Whenever any client needs any connection, it can request the connection and will return connection after use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!