Java: Threading Techniques & Concepts

When using streams, I sometimes visualize them as weaving three or more dimensional relationships between objects in a spatial context. This is not a common scenario, but for what I am doing, this is a useful way to think about it.

Are there any APIs that you use, what auxiliary threads?

Do you use threads in a way that is not conceptual, since a thread is a process?

+6
java multithreading visualization
source share
4 answers

Are there any APIs you use that help in the thread?

Do you mean appart from java.util.concurrent ? FunctionalJava got some constructs that help in parallel programming, as described in the multi-page tutorial starting out.

Do you use threads in a way that is not conceptual, since a thread is a process?

Yes, to the extent that flows are not conceptualized at all. Take, for example, an asynchronous runner task. He uses streams under covers, but I donโ€™t see them, and I donโ€™t care about them. They are fully controlled by the mission commander.

Under the covers, all these are just threads, but when we stop caring for a separate thread and just think of them as several slots, where you can somehow enter the code and run it for a certain period of time, then when we start to reach higher level of abstraction.

Agents / Actors are a common way to do this. An actor is like a thread that has a piece of state, and then you can send it some code and say โ€œdo it in your state when you have timeโ€ or something like that.

+14
source share

First of all

Regular disclaimer: programming in any language at the same time, using any level of abstraction, is hard and complex and has many risks. Take into account:

  • Concurrent programming complicates any size application
  • Critical Partition Test Modules are Tough and Sometimes Impossible
  • Reproduction of errors that occur in parallel code is very difficult and highly dependent on the architecture, OS style, version, etc.

Java Concurrent API

Java has come a long way in making concurrent programming easier for developers. In most cases, you will see that java.util.concurrent has most of the abstractions you will need:

  • Runnable and Thread object that you can expand. Just enter the code and you have a thread ready to run.
  • A good set of Executors : persistent pool, dynamic pool, scheduled or any other. Just throw Runnable at it, and everything else.
  • Semaphore s and locks of all kinds will eliminate the need for common locking methods.
  • Built-in wait() and notify() API for all objects.

Using

The only thing left for you as a software engineer is to make sure that you are writing the correct one . So you should be aware of dangerous situations that you could expose yourself to:

  • A dead end is a situation where two or more threads are waiting on unordered resources, providing an endless waiting cycle.
  • Livelock - two or more threads that politely try to concede to each other on a shared resource, but ultimately do not accept it (consider two people in the corridor going to each other and constantly moving together from side to side)
  • Starvation is a single thread that occupies most or all of one common resource, thereby depriving other threads of access to it.

The main point (or when to use it)

Use threads only when concurrency will directly improve the behavior of your applications.

If you expect a resource related to IO / network / hardware, DO , create a thread to continue with other activities.

If you are just trying to elegantly separate the processor related calculations, DO NOT use , use threads. You can simply degrade your performance.

If you use threads, make sure that you carefully consider the risks and check three times, you did not miss any exceptional situations.

Useful (online) resources

The quickest way to get into things is to do a Sun concurrency tutorial . Other than that, get a good book.

Good luck :)

+8
source share

Concurrency is a deep and complex topic. Books like Java Concurrency in practice can help.

See Concurrency Utility Overview for Streaming APIs. BlockingQueue <E> may be useful, for example.

A queue that additionally supports operations, pending queues become empty when an item is received, and wait until space becomes available in the queue when an item is stored.

See CountDownLatch

Support for synchronization, allowing one or more threads to wait until operations performed on other threads are complete.

and CyclicBarrier for some interesting behavior.

Synchronization help, which allows multiple threads for everyone to wait for each other to reach a common barrier point.

Edit : Now I am reading Java Concurrency. It is very good.

+3
source share

When using streams, I sometimes visualize them as weaving three or more dimensional relationships between objects in a spatial context.

It sounds complicated, how would you conceptualize 600 streams, for example? Why not think about them, since several threads of execution seem to be running at the same time.

Are there any APIs that you use, what kind of help streams?

I suggest that the simplest and most straightforward ones are the first matches you find. http://www.google.co.uk/search?q=java+threads

Do you use threads in a way that is not conceptual, since a thread is a process?

Since threads are not processes, I cannot say that I have ever thought of threads as processes. (Except for older versions of Linux) Processes do not share default memory / objects to run, they run completely independently (usually different programs, possibly written in different languages). They also begin to use different APIs in different ways.

It is believed that multithreading is complex. In fact, I would say the opposite. Multithreaded programming requires your code to be simple, straightforward, and straightforward. Although this requires experience, your goal is simplicity.

0
source share

All Articles