The document discusses Java performance and threading. It provides an overview of performance concepts, the performance process, and measurement techniques like benchmarking and profiling. It also covers key threading concepts like thread states, synchronization, and how to share data across threads using synchronized methods, objects, and wait/notify.
This session discusses about the basic building blocks of Concurrent Programming in Java, which include:
synchronisation part 2, synchronized methods, intrinsic locks, atomic access, liveness, deadlock, starvation, livelock, guarded blocks, immutable objects, synchronized class example, strategy for defining immutable objects
Advanced Introduction to Java Multi-Threading - Full (chok)choksheak
Designed for the beginning Java developer to grasp advanced Java multi-threading concepts quickly. Talks mainly about the Java Memory Model and the Concurrent Utilities. This presentation is Java-specific and we intentionally omit general non-Java-specific details, such as hardware architecture, OS, native threads, algorithms, and general software design principles etc.
Threads And Synchronization in C#
Concept about Threads And Synchronization in C#
it will help you about this concept
feel free
Download free programming applications on
http://www.androidapplications.xyz
Multithreading is the ability of a program or an
operating system process to manage its use by
more than one user at a time and to even manage
multiple requests by the same user without
having to have multiple copies of the
programming running in the computer.
This session discusses about the basic building blocks of Concurrent Programming in Java, which include:
synchronisation part 2, synchronized methods, intrinsic locks, atomic access, liveness, deadlock, starvation, livelock, guarded blocks, immutable objects, synchronized class example, strategy for defining immutable objects
Advanced Introduction to Java Multi-Threading - Full (chok)choksheak
Designed for the beginning Java developer to grasp advanced Java multi-threading concepts quickly. Talks mainly about the Java Memory Model and the Concurrent Utilities. This presentation is Java-specific and we intentionally omit general non-Java-specific details, such as hardware architecture, OS, native threads, algorithms, and general software design principles etc.
Threads And Synchronization in C#
Concept about Threads And Synchronization in C#
it will help you about this concept
feel free
Download free programming applications on
http://www.androidapplications.xyz
Multithreading is the ability of a program or an
operating system process to manage its use by
more than one user at a time and to even manage
multiple requests by the same user without
having to have multiple copies of the
programming running in the computer.
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Sachintha Gunasena
This session discusses about the basic building blocks of Concurrent Programming in Java, which include:
processes, threads, how to define a thread, how to start a thread, how to sleep a thread, thread interrupt, interrupt status flag, joins, simple thread example, synchronization part 1, thread interference, memory consistency errors
This presentation is about advanced multithreading and concurrency in Java. I have tried my best to explain the concepts with code. Feel free to reach me if you have any questions or concerns.
Applet Basics,
Applet Organization and Essential Elements,
The Applet Architecture,
A Complete Applet Skeleton,
Applet Initialization and Termination,
Requesting Repainting
The update() Method,
Using the Status Window
Passing parameters to Applets
The Applet Class
Event Handling The Delegation Event Model
Events,
Using the Delegation Event Model,
More Java Keywords.
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Sachintha Gunasena
This session discusses about the basic building blocks of Concurrent Programming in Java, which include:
processes, threads, how to define a thread, how to start a thread, how to sleep a thread, thread interrupt, interrupt status flag, joins, simple thread example, synchronization part 1, thread interference, memory consistency errors
This presentation is about advanced multithreading and concurrency in Java. I have tried my best to explain the concepts with code. Feel free to reach me if you have any questions or concerns.
Applet Basics,
Applet Organization and Essential Elements,
The Applet Architecture,
A Complete Applet Skeleton,
Applet Initialization and Termination,
Requesting Repainting
The update() Method,
Using the Status Window
Passing parameters to Applets
The Applet Class
Event Handling The Delegation Event Model
Events,
Using the Delegation Event Model,
More Java Keywords.
Concurrency and Multithreading Demistified - Reversim Summit 2014Haim Yadid
Life as a software engineer is so exciting! Computing power continue to rise exponentially, software demands continue to rise exponentially as well, so far so good. The bad news are that in the last decade the computing power of single threaded application remains almost flat.
If you decide to continue ignoring concurrency and multi-threading the gap between the problems you are able to solve and your hardware capabilities will continue to rise. In this session we will discuss different approaches for taming the concurrency beast, such as shared mutability,shared immutability and isolated mutability actors, STM, etc we will discuss the shortcomings and the dangers of each approach and we will compare different programming languages and how they choose to tackle/ignore concurrency.
Lecture 10 from the IAG0040 Java course in TTÜ.
See the accompanying source code written during the lectures: https://github.com/angryziber/java-course
This presentation introduces Java Applet and Java Graphics in detail with examples and finally using the concept of both applet and graphics code the analog clock project to depict how to use them in real life challenges and applications.
Multithreading and concurrency in androidRakesh Jha
Here you will learn -
What is Multithreading
What is concurrency
Process Vs Thread
Improvements and issues with concurrency
Limits of concurrency gains
Concurrency issues
Threads pools with the Executor Framework
AsyncTask and the UI Thread
Code
Java assigns each thread a priority that determines how that thread should be treated for the others. Thread priorities are integers that specify the relative importance of one thread to another.
A priority is meaningless as an absolute value; a higher priority thread does not run any faster than a lower-priority thread if it is the only thread running. Instead, a thread’s priority is used to decide when to switch from one running thread to next.
16. class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; } public void run() { for (;;) { System.out.println(name + ": hello world"); } } } public class Main2 { public static void main(String [] args) { MyThread t1 = new MyThread("thread1"); MyThread t2 = new MyThread("thread2"); t1.start(); t2.start(); } }
17. thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread1: hello world thread2: hello world thread1: hello world thread2: hello world thread2: hello world thread1: hello world thread2: hello world thread2: hello world
18.
19. class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; } public void run() { for (;;) { System.out.println(name + ": hello world"); yield(); } } } public class Main3 { public static void main(String [] args) { MyThread t1 = new MyThread("thread1"); MyThread t2 = new MyThread("thread2"); t1.start(); t2.start(); } }
27. class SharedData { public int a = 0; public String s = null; public SharedData() { a = 10; s = "Test"; } } class MyThread extends Thread { private SharedData m_data = null; public MyThread(SharedData data) { m_data = data; } public void run() { for (;;) { m_data.a++; } } }
28. public class Main5 { public static void main(String [] args) { SharedData data = new SharedData(); MyThread t1 = new MyThread(data); t1.start(); for (;;) { data.a--; } } } If we have multiple threads accessing this shared data, how do we synchronize access to ensure it remains in a consistent state?
29.
30.
31.
32.
33. This implementation has a problem! The Consumer prints which slows it down a LOT, and thus the producer is faster, and thus the producer fills up the queue, and causes heap space to run out!! A good exercise here is to alter this example to limit the maximum number of items that are stored in the queue. See BoundedSynchMain.java
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48. Exception in Wait // In a synchronized method // check your condition, e.g., with a semaphore // operation, test “value” member variable if /* or while */ (/* condition */) { boolean interrupted; do { interrupted = false; try { wait(); } catch (InterruptedException e) { interrupted = true; } } while (interrupted); } Only allows release from wait caused by notify or notifyAll
49.
50.
51.
52.
53.
54.
55.
56.
57. Fine Grained Locks class FineGrainLock { MyMemberClass x, y; Object xlock = new Object(), ylock = new Object(); public void foo() { synchronized(xlock) { //access x here } //do something here - but don't use shared resources synchronized(ylock) { //access y here } } public void bar() { synchronized(xlock) { synchronized(ylock) { //access both x and y here } } //do something here - but don't use shared resources } }
58. Reducing Lock Granularity Another valuable technique for reducing contention is to spread your synchronizations over more locks. For example, suppose that you have a class that stores user information and service information in two separate hash tables, as shown in example. public class AttributesStore { private HashMap usersMap = new HashMap(); private HashMap servicesMap = new HashMap(); public synchronized void setUserInfo(String user, UserInfo userInfo) { usersMap.put(user, userInfo); } public synchronized UserInfo getUserInfo(String user) { return usersMap.get(user); } public synchronized void setServiceInfo(String service, ServiceInfo serviceInfo) { servicesMap.put(service, serviceInfo); } public synchronized ServiceInfo getServiceInfo(String service) { return servicesMap.get(service); } }
59. Reducing Lock Granularity http://www.ibm.com/developerworks/java/library/j-threads2.html Here, the accessor methods for user and service data are synchronized, which means that they are synchronizing on the AttributesStore object. While this is perfectly thread-safe, it increases the likelihood of contention for no real benefit. If a thread is executing setUserInfo, it means that not only will other threads be locked out of setUserInfo and getUserInfo, as is desired, but they will also be locked out of getServiceInfo and setServiceInfo. This problem can be avoided by having the accessor simply synchronize on the actual objects being shared (the userMap and servicesMap objects), as shown. Now threads accessing the services map will not contend with threads trying to access the users map. (In this case, the same effect could also be obtained by creating the maps using the synchronized wrapper mechanism provided by the Collections framework, Collections.synchronizedMap.) Assuming that requests against the two maps are evenly distributed, in this case this technique would cut the number of potential contentions in half. public class AttributesStore { private HashMap usersMap = new HashMap(); private HashMap servicesMap = new HashMap(); public void setUserInfo(String user, UserInfo userInfo) { synchronized(usersMap) { usersMap.put(user, userInfo); } } public UserInfo getUserInfo(String user) { synchronized(usersMap) { return usersMap.get(user); } } public void setServiceInfo(String service, ServiceInfo serviceInfo) { synchronized(servicesMap) { servicesMap.put(service, serviceInfo); } } public ServiceInfo getServiceInfo(String service) { synchronized(servicesMap) { return servicesMap.get(service); } } }
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
Editor's Notes
Next time add Java Conditions into this in synchronization part http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/Condition.html
Don’t call run method directly.
Run on Mac OS X, 3/14/08
Run on Mac OS X, 3/14/08
final on a method indicates it can’t be overridden The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing, t.join(); causes the current thread to pause execution until t's thread terminates. Overloads of join allow the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify. Like sleep, join responds to an interrupt by exiting with an InterruptedException.
10 and 1 were obtained on Mac OS X 2/21/05
Of course, part of this depends on how we define “consistent state”.
Should change this to be a generic type. Have them notice that the constructor is not synchronized. Why is that? It is because no two threads can call the constructor and get the same object returned.
I’ve marked the changes to the previous version in red.
Another example of checking/rechecking conditions: Let them work this one in class
See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#wait() for reasons why wait can be woken “ if another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.” See also: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#interrupt()
Another implementation of this without the complex code for dealing with the InterruptedException would be to have the V operation increment a variable, and have the P operation test that variable to see if it is > 0. See Semaphore-v3.java