cs427-10

cavalcadejewelSoftware and s/w Development

Nov 18, 2013 (3 years and 8 months ago)

121 views

1

Concurrency: Introduction

427
-
10

2

MP3


Due on Tuesday, September 29


Fair warning: Harder than MP2!


Refactorings


You can do some manually but try to use Eclipse
to automate that (our tests only 2
-
4 manual)


You don’t have to worry about order of methods
or comments or white spaces


Concurrency


We’ll cover the basics today

427
-
10

3

Sample exams


Exams from previous years on Wiki

https://agora.cs.illinois.edu/display/cs427fa09/Midterm+Exam

https://agora.cs.illinois.edu/display/cs427fa09/Final+Exam


Note that material in cs427 used to be in
cs428 before 2007


Advice


Skim/read the exams soon to see the format


Keep up on reading

Please skim to
see the format

427
-
10

4

Intro to Concurrency


Concurrency in Java


Reading from Sun

http://java.sun.com/docs/books/tutorial/essential/concurrency/


Covers both older “low
-
level API” and
newer “high
-
level API”


We will work mostly with “low
-
level API”


MP3 is on some basics of concurrency


MP4 will be on finding bugs with JPF

427
-
10

5

Concurrency


Several computations going on at a time


Sometimes part of requirements


Example: GUI systems (CS 465)


Sometimes done for performance


Parallel systems (CS 420)


Sometimes in networked environments


Distributed systems (CS 425)

427
-
10

6

Concurrency in Java


Support since first version of Java


Processes (we won’t look into that)


Communicate with interprocess communication


Threads (we’ll focus on this)


Communicate through shared memory accesses


More recent support


Executors, fork
-
join (we won’t look into that)


427
-
10

7

java.lang.Thread


Basic class for threads in Java


All (started) objects of this class can be
running concurrently


Note that they may be slicing on one core,
so no true concurrency


Need to provide code for the thread


Runnable object (preferred)


Implement run method (shorter to write)

427
-
10

8

Example from reading

public class HelloThread extends Thread {


public void run() {


System.out.println("Hello from a thread!");


}


public static void main(String args[]) {


(new HelloThread()).start();


}

}

427
-
10

9

Two threads (one main)

public class HelloThread extends Thread {


public void run() {


System.out.println("Hello from a thread!");


}


public static void main(String args[]) {


(new HelloThread()).start();


System.out.println("Hello from the main!");


}

}

427
-
10

10

Ordering


What orders are possible?


Thread before main


Hello from a thread!


Hello from the main!


Thread after main


Hello from the main!


Hello from a thread!

427
-
10

11

Actual execution


Repeated executions may always follow
the same/similar ordering


Bugs can occur very infrequently


“Heisenbugs”: gone when you observe them


Methods that can affect ordering


Thread.yield


give up on the processor


Thread.sleep


wait for some time


JPF allows exploring these orderings


No need to use yield/sleep

427
-
10

12

Atomicity


How about interleaved printing?


Could we get the following?


Hello from the

thread!


Hello from a
main!


Or how about this


Hello from
Hello from
a thread!


the main!


Atomicity: either all or nothing

427
-
10

13

Split operations

public class HelloThread extends Thread {


public void run() {


System.out.print("Hello from ");


System.out.println("a thread!");


}


public static void main(String args[]) {


(new HelloThread()).start();


System.out.print
("Hello from ");


System.out.println
("the main!");


}

}

427
-
10

14

More interleavings


Can we now get the following?


Hello from
Hello from
a thread!


the main!


How many interleavings are there?


t1 t1 t1 three more that start with m1


t2 m1 m1


m1 t2 m2


m2 m2 t2


JPF allows exploring these interleavings

427
-
10

15

Seemingly atomic code

class Counter {


private int c = 0;


public void increment() { c++; }


public void decrement() { c
--
; }


public int value() { return c; }

}


What could go wrong with multithreading?

427
-
10

16

Two threads sharing counter


What could go wrong with multithreading?


final

Counter c =
new

Counter();


Thread
t1

=
new

Thread() {


public

void

run() {


c.increment();


}


};


Thread
t2

=
new

Thread() {


public

void

run() {


c.increment();


}


};


System.
out
.println(c.value());

427
-
10

17


Starting/ending threads


We missed t1.start() and t2.start()


We missed t1.join() and t2.join()


Method Thread.join


wait for thread to
finish execution



Still, how can we ensure to get value 2?

427
-
10

18


Synchronization


Use locks to make operations atomic


Synchronized methods


The entire body is atomic


Lock is on “this” object


Problems if other objects are involved


Synchronized statement


Grab lock on some object


Same example with SynchronizedCounter

427
-
10

19

Common concurrency bugs


Data races: two threads accessing the
same data, at least one of those
accesses is a write


Atomicity violation: some things that
should have been executed together
were not executed together


Deadlocks: everything gets blocked


Livelocks: no useful gets done

427
-
10

20

Next: JPF&Concurrency


JPF can be used to find some
concurrency bugs


Concurrency leads to non
-
determinism


More generally, JPF can explore programs
with non
-
determinism (even just for data)