简体   繁体   中英

how to block a statement from execution till the previuos has finished in java?

I have two lines :

   display.print(x); // outputs a collection contenet
   LOG.info("Total word count is completed.");

the method call in the first line prints a bunch of data from a collection. But while its printing and before finishing the log statement gets executed. So the output looks like:

...   
a : 6
as : 6
23/06/2012 09:38:01 م sample.testing
INFO: Processing... 0 thread(s) has/have finished
book : 6
had : 6
...

but the log should only be printed after all the data is printed. why this is happening? and how it can be solved?

that's the print method:

 public void print(Map<String, Integer> map)
{
    for (Map.Entry<String, Integer> current : map.entrySet())
    {
        System.out.println(String.format("%s : %d", current.getKey(), current.getValue()));
    }
    System.out.println();
}

edit 2: I pass ThreadClass.getCol() to the print method. ThreadClass.getCol() returns a collection from ThreadClass where many threads update this collection. It's synchronized and i am returning a copy of it.

Presumably outputter.print fires off a thread (or maybe multiple threads), which is/are asynchronously writing to System.out (or wherever your text output is going).

The way to solve this is to wait until these threads have completed all their work. As you haven't shown us the code for that method, I can't really help you with that!

If @Oli Charlesworth is correct, and outputter.print() fires off a thread, we will need to see that code and find out how to wait for it to return.

Another possibility is that outputter.print() doesn't do anything fancy, but that two different threads are calling this code at once. In that case, the simplest fix is to synchronize this code. eg, if it is in a method:

public **synchronized** void outputterPrint() {
   outputter.print(x); // outputs a collection contenet
   // you might want to add some sort of flush here
   outputter.flush();

   LOG.info("Total word count is completed.");
}

If this code gets called a skillion times this has performance issues. But for infrequent calls this is safe and simple.

outputter.print fires starts a porcess or a thread. If outputter.print(x) returns a Process, you could use .waitFor() to wait for the end. But its not enough information to answer your question. Perhaps theres a method which you tell, if its ready. If you wanna do a dirty trick, use Thread.currentThread().sleep(1000)

But its not a good solution

在打印语句后,尝试对输出器执行刷新操作。

there is a multi-threading issue though I can't be sure why cause you haven't placed your entire code. You could get your hands dirty by "getting" the appropriate thread and adding a NotifyAll() and then causing another thread to execute by waiting for this notification.

OR you could just place your

LOG.info("Total word count is completed.");

at the end of your print (...) method.

Comment if you want an abstract answer for your original question "how to block a statement from execution till the previuos has finished in java?"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM