SCJP 9/10 Threads.

Slides:



Advertisements
Similar presentations
Concurrency 101 Shared state. Part 1: General Concepts 2.
Advertisements

CS 5704 Fall 00 1 Monitors in Java Model and Examples.
COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
© 2002 by Ashby M. Woolf Revision 3 Java Threads Teaching your programs to walk and chew gum at the same time.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
28-Jun-15 Producer-Consumer An example of using Threads.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
Multithreading.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Lecture 4 : JAVA Thread Programming
Java Programming: Advanced Topics
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
111 © 2002, Cisco Systems, Inc. All rights reserved.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Threading Eriq Muhammad Adams J
Threads.
Synchronizing threads, thread pools, etc.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science.
1 5.0 Garbage collector & Threads : Overview Introduction: In this module we discuss the merits and demerits of automated garbage collection over manual.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Threading and Concurrency COM379T John Murray –
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Multi-Threading in Java
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Asynchronous Programming Writing Asynchronous Code in Java SoftUni Team Technical Trainers Software University
Java Thread Programming
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multi Threading.
Multithreaded Programming in Java
Java Concurrency.
Section 5.7 Concurrency, Interference, and Synchronization.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Java Concurrency.
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Java Threads אליהו חלסצ'י תכנות מתקדם תרגול מספר 6
Multithreading 2 Lec 24.
Multithreading.
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
برنامه‌نویسی چندنخی Multi-Thread Programming
21 Threads.
Computer Science 2 06A-Java Multithreading
Threads and Multithreading
CMSC 202 Threads.
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

SCJP 9/10 Threads

Exam objectives Start new threads Recognize thread states and transitions Use object locking to avoid concurrent access Write code that uses wait(), notify(), notifyAll()

Starting new thread java.lang.Thread Start() Yield() // yield – poddawać się, uginać się Sleep() Run() Extending vs. java.lang.Runnable

thread.run Thread t = new Thread(); t.run(); // legal but doesn't start a new thread

Many threads Runnable mr = new MyRunnable(); Thread t1 = new Thread(mr); Thread t2 = new Thread(mr); Thread t3 = new Thread(mr); t1.start(); t2.start(); t3.start();

The order in which threads are chosen to run is not guaranteed.

Thread states

Thread states Sleeping, waiting or blocked on object's lock new Thread() start() Scheduler decision After run()

Sleep() and yield() are static. They always affect current thread

Sleep(milisec) is the minimum duration in which thread will not run Sleep(milisec) is the minimum duration in which thread will not run. Can take longer

Priorities Don't rely on thread priorities (Thread.setPriority()). It's information for schedules. Thread get's the same priority as the one in which it was created Yield() - hold back to allow other threads of the same priority

Join Thread t = new Thread(); t.start(); t.join() What does this code do?

t.join() = „Join me (the current thread) to the end of t, so that t finishes before me

Synchonization Keyword synchronized Synchronizing methods or blocks void synchronized doStuff() {…} synchronized (object) {…} Synchronization based on object lock. Every object has only one lock.

Method vs. block int synchronized doStuff() { return 10; } Is equivalent to int doStuff() { synchronized (this) { return 10; }

Locking static methods Every class also has a lock. static synchronized int doStuff() { return 10; } Is equivalent to static int doStuff() { synchronized (MyClass.class) { return 10; }

„Thread-safeness” StringBuffer (StringBuilder is NOT) Collections.synchronizedList/Set/Map()

Not safe thread-safe collections List list = Collections.synchronisedList(new ArrayList()); // is this safe? If (list.size() > 0) { list.remove(0); }

Thread interaction wait() - tell others that I'm waiting notify()/notifyAll() - tell others that I can get back to work

Wait(), notify(), notifyAll() is always called from within a synchonized block

java.lang.Object. Be sure to know what comes from Object, Runnable and Threadow what comes from Object, Runnable and Thread

The End :-)

Thread get's the