The document discusses concurrency problems that arise with multi-core processors and large datasets. It examines options for handling concurrency like functional programming, Java's concurrency API, and thread-safe collections. Patterns for managing threads through thread pools and executors are presented. Examples show how to update shared values atomically to avoid race conditions. References are provided for further reading on Java concurrency.
10. Thead-Safe
private
Set<String>
ipAddressSet
=
new
HashSet<String>();
quarta-feira, 10 de outubro de 2012
11. Thead-Safe
private
Set<String>
ipAddressSet
=
new
HashSet<String>();
private
Set<String>
ipAddressSet
=
Collections.
synchonizedSet(new
HashSet<String>());
quarta-feira, 10 de outubro de 2012
12. Thead-Safe
private
Set<String>
ipAddressSet
=
new
HashSet<String>();
private
Set<String>
ipAddressSet
=
Collections.
synchonizedSet(new
HashSet<String>());
private
BlockingQueue<String>
ipAddress=new
LinkedBlockingQueue<String>();
quarta-feira, 10 de outubro de 2012
13. Thead-Safe
fast-fail iterators
private
Set<String>
ipAddressSet
=
new
HashSet<String>();
private
Set<String>
ipAddressSet
=
Collections.
synchonizedSet(new
HashSet<String>());
private
BlockingQueue<String>
ipAddress=new
LinkedBlockingQueue<String>();
weakly consistent iterators
quarta-feira, 10 de outubro de 2012
14. Thead-Safe
Outras
Coleções
ConcurrentHashMap,
CopyOnWriteArrayList
e
CopyOnWriteArraySet
quarta-feira, 10 de outubro de 2012
15. Executor
Executor
ExecutorService
ScheduledExecutorService
quarta-feira, 10 de outubro de 2012
16. Executor
(new Thread(r)).start();
quarta-feira, 10 de outubro de 2012
17. Executor
(new Thread(r)).start();
e.execute(r);
quarta-feira, 10 de outubro de 2012
18. Thread Pools
Executors.newCachedThreadPool();
Executors.newFixedThreadPoll(n);
Executors.newSingleThreadExecutor();
quarta-feira, 10 de outubro de 2012
19. Thread Pools
class Counter {
private int c = 0;
public void increment() {
c++;
}
public void decrement() {
c--;
}
public int value() {
return c;
}
}
1. Thread A: Retrieve c.
2. Thread B: Retrieve c.
1. Retrieve the current value of c.
3. Thread A: Increment retrieved value; result is 1.
2. Increment the retrieved value by 1.
4. Thread B: Decrement retrieved value; result is -1.
3. Store the incremented value back in c.
5. Thread A: Store result in c; c is now 1.
6. Thread B: Store result in c; c is now -1.
quarta-feira, 10 de outubro de 2012
20. Thread Pools
class SynchronizedCounter {
private int c = 0;
public synchronized void increment() {
c++;
}
public synchronized void decrement() {
c--;
}
public synchronized int value() {
return c;
}
}
quarta-feira, 10 de outubro de 2012
21. Thread Pools
import java.util.concurrent.atomic.AtomicInteger;
class AtomicCounter {
private AtomicInteger c = new AtomicInteger(0);
public void increment() {
c.incrementAndGet();
}
public void decrement() {
c.decrementAndGet();
}
public int value() {
return c.get();
}
}
quarta-feira, 10 de outubro de 2012
22. Bibliografia
Livros
Concurrent Programming in Java, Design Principles and Patterns, Doug Lea, Addison-Wesley, 1999
Java Concurrency In Practice, Brian Goetz, Addison-Wesley, 2006
Links
http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html
http://www.jcp.org/en/jsr/detail?id=166
http://gee.cs.oswego.edu/dl/concurrency-interest/
http://www.devmedia.com.br/websys.2/webreader.asp?cat=6&revista=javamagazine_69
quarta-feira, 10 de outubro de 2012