Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Java Session 3 New York University School of Continuing and Professional Studies.

Similar presentations


Presentation on theme: "Advanced Java Session 3 New York University School of Continuing and Professional Studies."— Presentation transcript:

1

2 Advanced Java Session 3 New York University School of Continuing and Professional Studies

3 2 Objectives Revisit thread Synchronization issues –Updated versions of SynchBankTest and UnsynchBankTest Collection Interfaces Concrete Collections Collection Framework Algorithms Legacy Collections

4 3 Synchronization Issues Race condition – producer-consumer –When multiple threads try to manipulate the same object, they may get wrong results. Deadlock – dining philosophers –A thread going after a resource that’s held by another thread, while the other thread needs the resource held by this thread, hence both wait endlessly.

5 4 Synchronization Examples UnsynchBankTest.java –TransferThread –TestThread SynchBankTest.java –TransferThread –TestThread

6 5 Interfaces You cannot instantiate an object of type Interface with new. You can declare that an object variable will be of that interface type. You can use “instanceof” to check if an object implements an interface. You cannot put instance fields, or static methods in interfaces. You can put constants in interfaces.

7 6 Implementations A class may implement one or more Interfaces using the keyword “implements” ----- public interface Comparable { public int compareTo(Object b) } ------ Class Employee extends Person implements Comparable

8 7 Queue Example 1234 head tail Interface Queue { void add(Object obj); Object remove(); int size(); }

9 8 Circular Array Implementation head tail 9 10 6 7 8

10 9 Linked List Implementation headtail 1234

11 10 Sort program import java.util.*; public class sort { public static void main(String args[]) { List l = Arrays.asList(args); Collections.sort(l); System.out.println(l); }

12 11 Collection Interface Has three fundamental methods –boolean add (Object obj) –boolean remove (Object obj) –Iterator iterator() Other methods –boolean contains(Object obj) –boolean size() –boolean isEmpty() –boolean retainAll(Collection c) –boolean containsAll(Collection c)

13 12 Iterator Interface Has three fundamental methods –Object next() –boolean hasNext() –void remove() Element 1 Element 2 Element 3 Element 4 Element 5 next()

14 13 Concrete Collections Linked Lists Array Lists Hash Sets Tree Sets Maps Week Hash Maps 10 20 30 40 141719 33 4245

15 14 Algorithms/Utility methods in Collections class Common set of methods that work on all types of Collections and Lists min, max – can work on any Collection sort, binarySearch, shuffe, reverse, copy, fill – can work on any List unmodifiable and synchronized versions of Collections

16 15 Legacy Collections Hashtable Enumeration Properties


Download ppt "Advanced Java Session 3 New York University School of Continuing and Professional Studies."

Similar presentations


Ads by Google