Recently I found when I use a boolean value in multiple threads, it is not thread-safe enough. For example, in one thread you are writing values for a boolean value, in another thread you are checking the boolean in order to process the thread. How does the program know the value when you check? This suitation also happens in other values not limited to boolean.

We should aviod checking and writing a value in multiple thread in the same time. Here is a simple solution I found: use java.util.concurrent.atomic package. Atomic Collections support lock-free thread-safe programming on single variables.

Continue reading

Remember in article synch/Asynch Callback in Java I wrote, multithreading in Java is important and can improve performance. We can use callbacks to notify some threads finish executing. What if we don’t need to pass arguments, but we still want to implements multithreads falixable? Another simple way to let a thread wait and wake up is to use Object.wait() and Object.notify().

Keep in mind that:

  • Object.wait() means suspend a thread.
  • Object.notify() means wake a thread up.
Continue reading

Callback Function

Image a senario, you have several threads executed at the same time, how can you know some threads are already finished? Maybe you want to execuate a thread after another thread, then how can you control the flow? Here, we can use callback functions.

A callback function is a function passed into another function as an argument. The purpose of it is to inform a class Sync/Async if some work in other class is down.

In java, we can implement calbaks by interface. Here is the steps:

Continue reading
  • page 1 of 1
Author's picture

Amy Zhu

To learn without thinking is blindness, to think without learning is idleness.


Mapsted Software Engineer


Toronto, ON