• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Concurrency Doubt  RSS feed

 
RatiKanta pal
Ranch Hand
Posts: 88
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have to codes.One file write with Java Concurrency and without using Java Concurrency.




Without Concurrency:


after executing The code which using parallel execution is giving time 96750 millisecond .. And without using parallel execution is giving time 51032 millisecond.

Can anyone explain what wrong I am doing here.I am writing same amount of line for both the program 65536000.Thank you for the help in advance.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RatiKanta pal wrote:Can anyone explain what wrong I am doing here.Thank you for the help in advance.

What you are doing wrong is assuming that concurrency means 'runs faster'. It isn't the case. It means 'runs at the same(ish) time'. The problem is that if you have a resource which can't handle multiple threads at the same time, then your multiple threaded code will likely run slower - because it is doing the work of 1 task at a time and adding the overhead of switching contexts.

And that is what is happening in your case. You are writing to a disk and your disk doesn't have multiple write-heads. So only one thread can write at a time. Switching contexts between threads when they are writing to a disk is very expensive because it requires the disk to spin to the correct position (each thread will be writing to different locations, probably not all that close together) and that spinning of the disk as the thread context change takes a lot of time.

So you are normally much better off writing to files all in one thread unless you have a system which has multiple write-heads (like RAIDs) or decent disk buffers.
 
RatiKanta pal
Ranch Hand
Posts: 88
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose my basic is still not clear about concurrency.Thanks a lot for rectifying the problem.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RatiKanta pal wrote:I suppose my basic is still not clear about concurrency.

My basic rule of thumb is: Avoid it if you can. And the first thing you need to understand is why you use it.

The fact is that, despite all of Java's efforts to make it easier to understand, multi-threading is fiddly, error-prone, difficult to test, and SLOW, because of the need to synchronise all access to critical code and variables.

What designers hope is that by giving 10 threads a chance to do something, there's a better chance that at least one of them will be able to get on with its job instead of blocking - and in some cases it's true - but very often, it's more trouble than it's worth. There are also many alternatives to multi-threading so, before you go down that route, you need to be certain that you really want it, or you can prove that it will actually provide some benefit.

HIH

Winston
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
My basic rule of thumb is: Avoid it if you can. And the first thing you need to understand is why you use it.
(...)

Whilst I agree with what you write, I have nevertheless some trouble with the above sentence.
Namely: it ignores the importance of gaining experience and knowledge with respect
to the complexities of Concurrency. You only get that when writing such programs. In that
stadium, it is of no relevance if concurrency is of any use.

Later, when you understand the subject much better, then you are very much better
capable of judging whether to use concurrency or not, given some task at hand.

You see it in OP's question. He now experiences that concurrency may not give the
outcomes that were hoped for. And this is by far the best outcome he could ever
wish for.

Greetz,
Piet
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Whilst I agree with what you write, I have nevertheless some trouble with the above sentence.
Namely: it ignores the importance of gaining experience and knowledge with respect
to the complexities of Concurrency. You only get that when writing such programs...

You're quite right, and I should probably have qualified it by saying: "By all means learn about multi-threading...". There's nothing wrong with learning.

In that stadium, it is of no relevance if concurrency is of any use.

Ah, now that's where we'll have to agree to disagree. It's a case of chicken and egg.

How do you know that you're going to need multi-threading - I don't like the term "concurrency" here, because it's an effect, not the cause - unless you know why you would need it?

Weirdly enough, one of the things that happens as you get more experienced, is that your code tends to become simpler; and I suspect it's part of the process of learning the real meaning of "separation of responsibility".
When you're starting out, things like concurrency and reflection seem really neat, but many of us have learned that probably the most important thing about them is to know when not to use them. And the answer - as it turns out - is: quite often.

Why write a piece of "concurrent" code if there's no need to?

A classic example is the Java collections framework. Almost all the collections defined in it are NOT thread-safe, despite the fact that a collection may well need to be accessed by more than one thread. But that's not the collection's responsibility. It's business is to be a collection.

If you need a thread-safe collection, you can use one of the 'synchronized...' methods in Collections (java.util.Collections). And if you find them too heavy-handed (which arguably they are), then is the time to think about writing your own, tailored, concurrent one - just one example being ConcurrentSkipListMap.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!