SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
Overview




                                          Let’s get the basics of multi-
                                          threaded programming in Java, and
                                          then write some simple programs.




                                                         An Introduction to
                                          Multi-threading
12   MARCH 2008   |   LINUX FoR YoU   |   www.openITis.com



                                                             cmyk
Overview




     I
             n a typical application like a word                     created in main).
             processor, many tasks need to be executed
             in parallel—for instance, responding to                 Asynchronous execution
             the user, checking spellings, formatting,               Note that created threads run ‘asynchronously’. This
             displaying, saving, etc. These tasks can                means that threads do not run sequentially (like function
run as different processes, sharing the resources                    calls); rather, the order of execution of threads is not
using inter-process communication. Multi-processing                  predictable. To understand this, let us extend our simple
is heavyweight and costly in terms of the system calls               program here:
made, and requires a process switch.
    An easier and cheaper alternative is to use threads.              class MyThread extends Thread {
Multiple threads can run in the context of the same                   public void loop() {
process and hence share the same resources. A context                 for(int i = 0; i < 3; i++) {
switch is enough to switch between threads, whereas a                 System.out.println(“In thread “ +
costly process switch is needed for processes.                        Thread.currentThread().getName() +
    Java has support for concurrent programming and                   “; iteration “ + i);
has support for multi-threading in both language and                  try {
library levels. In this article, we’ll cover the basics of            Thread.sleep((int)Math.random());
multi-threaded programming in Java and write some                     }
simple programs. We’ll also look at some difficult                    catch(InterruptedException ie) {
problems that can crop up when we write multi-                        ie.printStackTrace();
threaded code.                                                        }
                                                                      }
The basics                                                            }
A Java thread can be created in two ways: by
implementing the Runnable interface or by extending                   public void run() {
the Thread class. Both have a method called run. This                 System.out.println(“This is new thread”);
method will be called by the runtime when a thread                        this.loop();
starts executing.                                                     }
    A thread can be created by invoking the start method
on a Thread or its derived objects. This in turn calls the            public static void main(String args[]) throws Exception {
run method and keeps the thread in running state. Now                 MyThread mt = new MyThread();
let us look at a simple example using threads.                            mt.start();
                                                                          System.out.println(“This is the main Thread”);
 class MyThread extends Thread {                                          mt.loop();
     public void run() {                                              }
             System.out.println(“This is new thread”);                }
       }
                                                                          In a sample run, the output was:
     public static void main(String args[]) throws Exception {
             MyThread mt = new MyThread();                            This is the main Thread
             mt.start();                                              In thread main; iteration 0
             System.out.println(“This is the main Thread”);           This is new thread
       }                                                              In thread Thread-0; iteration 0
 }                                                                    In thread Thread-0; iteration 1
                                                                      In thread main; iteration 1
     This program prints:                                             In thread Thread-0; iteration 2
                                                                      In thread main; iteration 2
 This is the main Thread
 This is new thread                                                      In this program, we have a loop method that has a for
                                                                     loop that has three iterations. In the for loop, we print
    In this example, the MyThread class extends the                  the name of the thread and the iteration number. We can
Thread class. We need to override the run method                     set and get the name of the thread using setName and
in this class. This run method will be called when the               getName methods, respectively. If we haven’t set the
thread runs. In the main function, we create a new                   name of the thread explicitly, the JVM gives a default
thread and start it. The program prints messages                     name (‘main’, ‘Thread-0’, ‘Thread-1’, etc.). After printing
from both the main thread and the mt thread (that we                 this information, we force the current thread to sleep for


                                                                              www.openITis.com   |   LINUX FoR YoU   |   MARCH 2008   13


                                                              cmyk
Overview



10 milli-seconds.                                                        of the value Counter.count and when they update
    The main thread (‘main’) and the child thread                        the counter with Counter.count++, they need not
(‘hread-0’) are executed independently. The output                       immediately reflect that value in the main memory. In
is not fixed and the order of executing the iterations                   the next read operation of Counter.count, the local
in the threads is not predictable. This is one of the                    value of Counter.count is printed.
fundamental and important concepts to understand in                          Technically, this program has a ‘data race’. To avoid
multi-threading.                                                         this problem, we need to ensure that the write and
                                                                         read operations are done together (‘atomically’) by a
Data races                                                               single thread. How do we ensure that? It is by acquiring
Threads share memory and they can concurrently                           a lock on the object. Only a single thread can acquire
modify data. This can lead to unintuitive results and end                a lock on an object at a time, and only that thread
in ‘data races’. Here is an example of a data race:                      can execute the code protected by the lock (that
                                                                         code segment is known as a ‘critical section’). Until
 class Counter {                                                         then the other threads have to wait. Internally, this is
 public static long count = 0;                                           implemented with monitors and the process is called as
 }                                                                       locking and unlocking.

 class UseCounter implements Runnable {                                  Thread synchronisation
 public void run() {                                                     Java has a keyword synchronised to acquire a lock. Lock
 for (int i = 0; i < 3; i++) {                                           is not obtained on code, but on a resource. Any object
             Counter.count++;                                            (not primitive types but of reference type) can be used
             System.out.print(Counter.count + “          “);             to acquire a lock.
     }                                                                       Here is an improved version of the code that provides
 }                                                                       synchronised access to Counter.count, and does both
 }                                                                       read and write operations on that in a critical section.
                                                                         For that, only the run method needs to be changed as
 public class DataRace {                                                 follows:
 public static void main(String args[]) {
 UseCounter c = new UseCounter();                                         public void run() {
     Thread t1 = new Thread(c);                                           for (int i = 0; i < 3; i++) {
     Thread t2 = new Thread(c);                                                        synchronized(this) {
     Thread t3 = new Thread(c);                                                                    Counter.count++;
     t1.start();                                                                               System.out.print(Counter.count + “   “);
     t2.start();                                                          }
     t3.start();                                                          }
 }                                                                        }
 }
                                                                            The program now prints the expected output
    In this program, we have a Counter class, which has                  correctly:
a static variable count. The UseCounter class simply
increments the count and prints the count three times in                  1    2   3   4   5   6   7   8   9
a for loop. We create three threads in the main function
in the DataRace class and start it. We expect the                            In the run method, we acquire lock on the implicit
program to print 1 to 9 sequentially as the threads run                  this pointer before doing both reading and writing to
and increment the counters. However, when we run this                    Counter.count.
program, it does print 9 integer values, but the output                      In this case, run is a non-static method. What about
looks like garbage! In a sample run, I got these values:                 static methods, as they do not have this pointer? You
                                                                         can obtain a lock for a static method, and the lock is
 3   3   5   6   3   7   8   4    9                                      obtained for the class instance and not the object. In fact,
                                                                         in this example, Counter.count is a static member, so we
   Note that the values will usually be different every                  might as well have acquired a lock on Counter.class for
time you run this program. What is the problem?                          modifying and reading Counter.count.
   The expression Counter.count++ is a write
operation and the next System.out.print statement                        Deadlocks
has a read operation for Counter.count. When the                         Obtaining and using locks is tricky and can lead to lots
three threads execute, each of them has a local copy                     of problems, one of which (a common one) is known as


14       MARCH 2008      |       LINUX FoR YoU   |   www.openITis.com



                                                                        cmyk
Overview



a ‘deadlock’ (there are other problems like ‘livelocks’                          Thread t2 = new Thread(c);

and ‘lock starvation’ that are not discussed here).                              t1.start();

    A deadlock happens when locking in threads                                   t2.start();

results in a situation where they cannot proceed and                 }

wait indefinitely for others to terminate -- for instance,           }

when one thread acquires a lock on resource, r1, and
waits to acquire another resource, r2. At the same                       If you execute this program, it might run fine, or it
time, there might be another thread that has already                may deadlock and never terminate.
acquired r2 and is waiting to obtain a lock on r1. Now,                  In this example, there are two classes,
as you will notice, both the threads cannot proceed till            Balls and Runs, with static member balls
the other releases the lock, which never happens, so                and runs. The Counter class has two
they ‘deadlock’.                                                    methods IncrementBallAfterRun and
    Here is an example that programmatically shows                  IncrementRunAfterBall. They acquire locks on Balls.
how this can happen.                                                class and Runs.class in the opposite order. The run
                                                                    method calls these two methods consecutively. The
 class Balls {                                                      main method in the Dead class creates two threads
 public static long balls = 0;                                      and starts them.
 }                                                                       When the threads t1 and t2 execute, they will
                                                                    invoke the methods IncrementBallAfterRun and
 class Runs {                                                       IncrementRunAfterBall. In these methods, locks are
 public static long runs = 0;                                       obtained in the opposite order. It might happen that t1
 }                                                                  acquires a lock on Runs.class and waits to acquire a
                                                                    lock on Balls.class. Meanwhile, t2 might have acquired
 class Counter implements Runnable {                                Balls.class and waits to acquire a lock on Runs.class.
       public void IncrementBallAfterRun() {                        So, this program can lead to a deadlock. It is not
         synchronized(Runs.class) {                                 assured that this program will lead to a deadlock every
                   synchronized(Balls.class) {                      time you execute this program. Why? We never know
                   Runs.runs++;                                     the sequence in which threads execute, and the order
                         Balls.balls++;                             in which locks are acquired and released. For this
 }                                                                  reason, the problems are said to be ‘non-deterministic’
 }                                                                  and such problems cannot be reproduced consistently.
 }                                                                       We will not look at how to resolve this deadlock
                                                                    problem in this article. The objective is to introduce
 public void IncrementRunAfterBall() {                              you to problems like these that can occur in multi-
 synchronized(Balls.class) {                                        threaded programs.
                   synchronized(Runs.class) {                            In this article, we explored the basics of writing
                   Balls.balls++;                                   multi-threaded programming in Java. We can create
                         Runs.runs++;                               classes that are capable of multi-threading by
 }                                                                  implementing a Runnable interface or by extending
 }                                                                  a Thread class. Concurrent reads and writes to
 }                                                                  resources can lead to ‘data races’. Java provides
                                                                    thread synchronisation features for protected access
       public void run() {                                          to shared resources. We need to use locks carefully.
 IncrementBallAfterRun();                                           Incorrect usage of lock can lead to problems like
 IncrementRunAfterBall();                                           deadlocks, livelocks or lock starvation, which are very
 }                                                                  difficult to detect and fix.
 }

                                                                     By: S G Ganesh is a research engineer in Siemens
 public class Dead {
                                                                     (Corporate Technology), Bangalore. His latest book is ‘60
       public static void main(String args[]) {
                                                                     Tips on Object Oriented Programming’ published by Tata
         Counter c = new Counter();
                                                                     McGraw-Hill in December 2007. You can reach him at
             Thread t1 = new Thread(c);
                                                                     sgganesh@gmail.com




                                                                           www.openITis.com    |   LINUX FoR YoU   |   MARCH 2008   15


                                                             cmyk

Weitere ähnliche Inhalte

Was ist angesagt?

Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAvasuraj pandey
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronizationcaswenson
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Inter threadcommunication.38
Inter threadcommunication.38Inter threadcommunication.38
Inter threadcommunication.38myrajendra
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadKartik Dube
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Javakoji lin
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamNavaneethan Naveen
 
Thread model of java
Thread model of javaThread model of java
Thread model of javamyrajendra
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and ConcurrencySunil OS
 

Was ist angesagt? (20)

Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Inter threadcommunication.38
Inter threadcommunication.38Inter threadcommunication.38
Inter threadcommunication.38
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Thread
ThreadThread
Thread
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
javathreads
javathreadsjavathreads
javathreads
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
Threading concepts
Threading conceptsThreading concepts
Threading concepts
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 

Andere mochten auch

Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Java multi threading
Java multi threadingJava multi threading
Java multi threadingRaja Sekhar
 

Andere mochten auch (6)

Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
 
interface in c#
interface in c#interface in c#
interface in c#
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Interface
InterfaceInterface
Interface
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 

Ähnlich wie Java Multithreading

Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024nehakumari0xf
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024kashyapneha2809
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAMaulik Borsaniya
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresHitendra Kumar
 
Medical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsMedical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsDaniel Blezek
 
Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programmingraksharao
 
Multithreading
MultithreadingMultithreading
Multithreadingbackdoor
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxArulmozhivarman8
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#Rizwan Ali
 

Ähnlich wie Java Multithreading (20)

18 concurrency
18   concurrency18   concurrency
18 concurrency
 
advanced java ppt
advanced java pptadvanced java ppt
advanced java ppt
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
 
Intake 38 12
Intake 38 12Intake 38 12
Intake 38 12
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
 
concurrency
concurrencyconcurrency
concurrency
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data Structures
 
Java multi thread programming on cmp system
Java multi thread programming on cmp systemJava multi thread programming on cmp system
Java multi thread programming on cmp system
 
Medical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsMedical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUs
 
Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programming
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Internet Programming with Java
Internet Programming with JavaInternet Programming with Java
Internet Programming with Java
 
Java
JavaJava
Java
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
 
Chap7 slides
Chap7 slidesChap7 slides
Chap7 slides
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 

Mehr von Ganesh Samarthyam

Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeGanesh Samarthyam
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”Ganesh Samarthyam
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGanesh Samarthyam
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionGanesh Samarthyam
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationGanesh Samarthyam
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterGanesh Samarthyam
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Ganesh Samarthyam
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckGanesh Samarthyam
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageGanesh Samarthyam
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Ganesh Samarthyam
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz QuestionsGanesh Samarthyam
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz QuestionsGanesh Samarthyam
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizGanesh Samarthyam
 

Mehr von Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 

Kürzlich hochgeladen

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Kürzlich hochgeladen (20)

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

Java Multithreading

  • 1. Overview Let’s get the basics of multi- threaded programming in Java, and then write some simple programs. An Introduction to Multi-threading 12 MARCH 2008 | LINUX FoR YoU | www.openITis.com cmyk
  • 2. Overview I n a typical application like a word created in main). processor, many tasks need to be executed in parallel—for instance, responding to Asynchronous execution the user, checking spellings, formatting, Note that created threads run ‘asynchronously’. This displaying, saving, etc. These tasks can means that threads do not run sequentially (like function run as different processes, sharing the resources calls); rather, the order of execution of threads is not using inter-process communication. Multi-processing predictable. To understand this, let us extend our simple is heavyweight and costly in terms of the system calls program here: made, and requires a process switch. An easier and cheaper alternative is to use threads. class MyThread extends Thread { Multiple threads can run in the context of the same public void loop() { process and hence share the same resources. A context for(int i = 0; i < 3; i++) { switch is enough to switch between threads, whereas a System.out.println(“In thread “ + costly process switch is needed for processes. Thread.currentThread().getName() + Java has support for concurrent programming and “; iteration “ + i); has support for multi-threading in both language and try { library levels. In this article, we’ll cover the basics of Thread.sleep((int)Math.random()); multi-threaded programming in Java and write some } simple programs. We’ll also look at some difficult catch(InterruptedException ie) { problems that can crop up when we write multi- ie.printStackTrace(); threaded code. } } The basics } A Java thread can be created in two ways: by implementing the Runnable interface or by extending public void run() { the Thread class. Both have a method called run. This System.out.println(“This is new thread”); method will be called by the runtime when a thread this.loop(); starts executing. } A thread can be created by invoking the start method on a Thread or its derived objects. This in turn calls the public static void main(String args[]) throws Exception { run method and keeps the thread in running state. Now MyThread mt = new MyThread(); let us look at a simple example using threads. mt.start(); System.out.println(“This is the main Thread”); class MyThread extends Thread { mt.loop(); public void run() { } System.out.println(“This is new thread”); } } In a sample run, the output was: public static void main(String args[]) throws Exception { MyThread mt = new MyThread(); This is the main Thread mt.start(); In thread main; iteration 0 System.out.println(“This is the main Thread”); This is new thread } In thread Thread-0; iteration 0 } In thread Thread-0; iteration 1 In thread main; iteration 1 This program prints: In thread Thread-0; iteration 2 In thread main; iteration 2 This is the main Thread This is new thread In this program, we have a loop method that has a for loop that has three iterations. In the for loop, we print In this example, the MyThread class extends the the name of the thread and the iteration number. We can Thread class. We need to override the run method set and get the name of the thread using setName and in this class. This run method will be called when the getName methods, respectively. If we haven’t set the thread runs. In the main function, we create a new name of the thread explicitly, the JVM gives a default thread and start it. The program prints messages name (‘main’, ‘Thread-0’, ‘Thread-1’, etc.). After printing from both the main thread and the mt thread (that we this information, we force the current thread to sleep for www.openITis.com | LINUX FoR YoU | MARCH 2008 13 cmyk
  • 3. Overview 10 milli-seconds. of the value Counter.count and when they update The main thread (‘main’) and the child thread the counter with Counter.count++, they need not (‘hread-0’) are executed independently. The output immediately reflect that value in the main memory. In is not fixed and the order of executing the iterations the next read operation of Counter.count, the local in the threads is not predictable. This is one of the value of Counter.count is printed. fundamental and important concepts to understand in Technically, this program has a ‘data race’. To avoid multi-threading. this problem, we need to ensure that the write and read operations are done together (‘atomically’) by a Data races single thread. How do we ensure that? It is by acquiring Threads share memory and they can concurrently a lock on the object. Only a single thread can acquire modify data. This can lead to unintuitive results and end a lock on an object at a time, and only that thread in ‘data races’. Here is an example of a data race: can execute the code protected by the lock (that code segment is known as a ‘critical section’). Until class Counter { then the other threads have to wait. Internally, this is public static long count = 0; implemented with monitors and the process is called as } locking and unlocking. class UseCounter implements Runnable { Thread synchronisation public void run() { Java has a keyword synchronised to acquire a lock. Lock for (int i = 0; i < 3; i++) { is not obtained on code, but on a resource. Any object Counter.count++; (not primitive types but of reference type) can be used System.out.print(Counter.count + “ “); to acquire a lock. } Here is an improved version of the code that provides } synchronised access to Counter.count, and does both } read and write operations on that in a critical section. For that, only the run method needs to be changed as public class DataRace { follows: public static void main(String args[]) { UseCounter c = new UseCounter(); public void run() { Thread t1 = new Thread(c); for (int i = 0; i < 3; i++) { Thread t2 = new Thread(c); synchronized(this) { Thread t3 = new Thread(c); Counter.count++; t1.start(); System.out.print(Counter.count + “ “); t2.start(); } t3.start(); } } } } The program now prints the expected output In this program, we have a Counter class, which has correctly: a static variable count. The UseCounter class simply increments the count and prints the count three times in 1 2 3 4 5 6 7 8 9 a for loop. We create three threads in the main function in the DataRace class and start it. We expect the In the run method, we acquire lock on the implicit program to print 1 to 9 sequentially as the threads run this pointer before doing both reading and writing to and increment the counters. However, when we run this Counter.count. program, it does print 9 integer values, but the output In this case, run is a non-static method. What about looks like garbage! In a sample run, I got these values: static methods, as they do not have this pointer? You can obtain a lock for a static method, and the lock is 3 3 5 6 3 7 8 4 9 obtained for the class instance and not the object. In fact, in this example, Counter.count is a static member, so we Note that the values will usually be different every might as well have acquired a lock on Counter.class for time you run this program. What is the problem? modifying and reading Counter.count. The expression Counter.count++ is a write operation and the next System.out.print statement Deadlocks has a read operation for Counter.count. When the Obtaining and using locks is tricky and can lead to lots three threads execute, each of them has a local copy of problems, one of which (a common one) is known as 14 MARCH 2008 | LINUX FoR YoU | www.openITis.com cmyk
  • 4. Overview a ‘deadlock’ (there are other problems like ‘livelocks’ Thread t2 = new Thread(c); and ‘lock starvation’ that are not discussed here). t1.start(); A deadlock happens when locking in threads t2.start(); results in a situation where they cannot proceed and } wait indefinitely for others to terminate -- for instance, } when one thread acquires a lock on resource, r1, and waits to acquire another resource, r2. At the same If you execute this program, it might run fine, or it time, there might be another thread that has already may deadlock and never terminate. acquired r2 and is waiting to obtain a lock on r1. Now, In this example, there are two classes, as you will notice, both the threads cannot proceed till Balls and Runs, with static member balls the other releases the lock, which never happens, so and runs. The Counter class has two they ‘deadlock’. methods IncrementBallAfterRun and Here is an example that programmatically shows IncrementRunAfterBall. They acquire locks on Balls. how this can happen. class and Runs.class in the opposite order. The run method calls these two methods consecutively. The class Balls { main method in the Dead class creates two threads public static long balls = 0; and starts them. } When the threads t1 and t2 execute, they will invoke the methods IncrementBallAfterRun and class Runs { IncrementRunAfterBall. In these methods, locks are public static long runs = 0; obtained in the opposite order. It might happen that t1 } acquires a lock on Runs.class and waits to acquire a lock on Balls.class. Meanwhile, t2 might have acquired class Counter implements Runnable { Balls.class and waits to acquire a lock on Runs.class. public void IncrementBallAfterRun() { So, this program can lead to a deadlock. It is not synchronized(Runs.class) { assured that this program will lead to a deadlock every synchronized(Balls.class) { time you execute this program. Why? We never know Runs.runs++; the sequence in which threads execute, and the order Balls.balls++; in which locks are acquired and released. For this } reason, the problems are said to be ‘non-deterministic’ } and such problems cannot be reproduced consistently. } We will not look at how to resolve this deadlock problem in this article. The objective is to introduce public void IncrementRunAfterBall() { you to problems like these that can occur in multi- synchronized(Balls.class) { threaded programs. synchronized(Runs.class) { In this article, we explored the basics of writing Balls.balls++; multi-threaded programming in Java. We can create Runs.runs++; classes that are capable of multi-threading by } implementing a Runnable interface or by extending } a Thread class. Concurrent reads and writes to } resources can lead to ‘data races’. Java provides thread synchronisation features for protected access public void run() { to shared resources. We need to use locks carefully. IncrementBallAfterRun(); Incorrect usage of lock can lead to problems like IncrementRunAfterBall(); deadlocks, livelocks or lock starvation, which are very } difficult to detect and fix. } By: S G Ganesh is a research engineer in Siemens public class Dead { (Corporate Technology), Bangalore. His latest book is ‘60 public static void main(String args[]) { Tips on Object Oriented Programming’ published by Tata Counter c = new Counter(); McGraw-Hill in December 2007. You can reach him at Thread t1 = new Thread(c); sgganesh@gmail.com www.openITis.com | LINUX FoR YoU | MARCH 2008 15 cmyk