1

I have this method:

public void start()
{

if(!isAclone())
{
...
this.thread.start()
}
else
{
...
this.thread.start()
}

I would execute one my method inside only if-block.wherever I put my method in start() method, it is execute twice.how can I achieve this problem?

EDIT

private final AtomicBoolean hasRun = new AtomicBoolean();

...
initializeLogger();
if(!hasRun.getAndSet(true))
{
   activateMonitoring();
}

....
user1508419
  • 333
  • 1
  • 4
  • 14
  • 3
    possibly duplicate: http://stackoverflow.com/questions/11382342/call-a-block-of-a-method-only-for-the-first-call-of-the-method – shem Jul 08 '12 at 12:23
  • It is not a duplicate.the situations are different... – user1508419 Jul 08 '12 at 12:23
  • 3
    Can you describe please how the situation is different? The answer given there does describe how to ensure a method is run only once. We're not sure what you need done differently, so it's hard to help. – VoteyDisciple Jul 08 '12 at 12:25
  • If you thing about a bit you will see how to use it in your situation – shem Jul 08 '12 at 12:25
  • Why don't you give more details on your scenario. Maybe you should change what you want instead of looking for a narrow solution. – Marko Topolnik Jul 08 '12 at 12:32
  • I do not see how this is different from the duplicate, unfortunately. – GETah Jul 08 '12 at 12:37
  • @MarkoTopolnik I will post the entire code so the thing will be more clearer – user1508419 Jul 08 '12 at 13:14
  • here it is all the class.I would execute activateMonitoring() only one time after initializeLogger() of the if in the start method...the solution give before from Marko Topolnik doesn't work – user1508419 Jul 08 '12 at 13:16
  • @GETah I posted the entire class – user1508419 Jul 08 '12 at 13:32
  • Sorry, but that's a whole lot of code. I still fail to see why you wouldn't be able to adapt my solution, in fact it could be even simpler. Why is `logger` not `static` so you reuse it automatically? And in `initializeLogger` you just check if it's `null` before initializing. – Marko Topolnik Jul 08 '12 at 13:54
  • The code wasn't written by me.it is jigsaw w3c project web server – user1508419 Jul 08 '12 at 14:01
  • Anyway I tried what you say me and it didn't work... – user1508419 Jul 08 '12 at 14:13
  • What exactly have you tried? The code you posted contains zero calls to `activateMonitoring`. – Marko Topolnik Jul 08 '12 at 14:20
  • @MarkoTopolnik In my application httpd thread is executed twice. what I need is that activateMonitoring is called at the second time when httpd thread is executed.So at the first time I don't need activateMonitoring.I need it at the second time.how can I do? – user1508419 Jul 08 '12 at 14:22
  • So you want to execute it **once** and want that once to happen the first time when `isClone == true`? Is that it? – Marko Topolnik Jul 08 '12 at 14:25
  • @MarkoTopolnik Right...exactly... – user1508419 Jul 08 '12 at 14:42
  • So why don't you insert `activateMonitoring()` at line 2129, and implement the method in the way described in my answer to your previous question? When done and it still doesn't work, paste your attempt at the solution and I'll try to have a look. Be sure to check the CURRENT version of the answer. – Marko Topolnik Jul 08 '12 at 14:45
  • @MarkoTopolnik I did what you told me to do but nothing...Unfortunately it didn't worked... – user1508419 Jul 08 '12 at 14:59
  • You don't intend to post the code showing your attempt? – Marko Topolnik Jul 08 '12 at 15:07
  • I posted the code...I edited the post... – user1508419 Jul 08 '12 at 15:10
  • I instructed you to check the CURRENT version of my answer. Please go back to it, refresh the page if needed, and implement it as written. To be more direct: you are missing the `static` modifier on the `AtomicBoolean`. That changes everything. – Marko Topolnik Jul 08 '12 at 15:30
  • @MarkoTopolnik yeeeessssss now it work... static changed everything...you are great...but it is not perfect as I would...I tought that when method start is executed for the second time, it enters to else clause instead at the second time it continue to enter in the if clause. I would execute activateMonitoring at the second time it enter to if clause...how can i do? – user1508419 Jul 08 '12 at 15:45
  • Replace `AtomicBoolean` with `AtomicInteger`, use `getAndIncrement` instead of `getAndSet`, and check for the exact value of 1 or 2, or whatever you make it to be at the second run. – Marko Topolnik Jul 08 '12 at 15:46
  • 1
    BTW after all the work I've had with this question, please at least **accept** my answer to the previous question. – Marko Topolnik Jul 08 '12 at 15:47
  • Obviously Marko...you are very nice... – user1508419 Jul 08 '12 at 15:53
  • @MarkoTopolnik and user: please add it as an answer and accept, so that we no longer have false "unanswered" question and you both gain rep. – Jarekczek Sep 17 '12 at 12:51
  • @jarekczek Out of curiosity... how did you end up at this question, it being of such narrow scope? It would be quite a bit of work to rephrase everything to a final answer, and I don't see enough value in it to do so---except if you make a good case for its value. – Marko Topolnik Sep 17 '12 at 13:05
  • Watching unanswered questions. I thought that copying the part after *EDIT* would be a sufficient answer. – Jarekczek Sep 17 '12 at 13:46

0 Answers0