introduction to programmingopenclassroom.stanford.edu/.../18-06-java-threads... · java threads...
TRANSCRIPT
![Page 1: Introduction to Programmingopenclassroom.stanford.edu/.../18-06-java-threads... · Java Threads •Java has concurrency built in through threads –Each thread has its own program](https://reader034.vdocument.in/reader034/viewer/2022042621/5f6bd6d52fb2b80f6460d6d8/html5/thumbnails/1.jpg)
Alex Aiken
Compilers
Java Threads
![Page 2: Introduction to Programmingopenclassroom.stanford.edu/.../18-06-java-threads... · Java Threads •Java has concurrency built in through threads –Each thread has its own program](https://reader034.vdocument.in/reader034/viewer/2022042621/5f6bd6d52fb2b80f6460d6d8/html5/thumbnails/2.jpg)
Alex Aiken
Java Threads
• Java has concurrency built in through threads – Each thread has its own program counter & stack
• Thread objects have class Thread
– Start and stop methods
• Synchronization obtains a lock on the object: synchronized (x) { e }
• In synchronized methods, this is locked
![Page 3: Introduction to Programmingopenclassroom.stanford.edu/.../18-06-java-threads... · Java Threads •Java has concurrency built in through threads –Each thread has its own program](https://reader034.vdocument.in/reader034/viewer/2022042621/5f6bd6d52fb2b80f6460d6d8/html5/thumbnails/3.jpg)
Alex Aiken
Java Threads
class Simple {
int a = 1, b = 2;
void to() { a = 3; b = 4; }
void fro() {println("a= " + a + ", b=" + b); }
}
Two threads call to() and fro(). What is printed?
![Page 4: Introduction to Programmingopenclassroom.stanford.edu/.../18-06-java-threads... · Java Threads •Java has concurrency built in through threads –Each thread has its own program](https://reader034.vdocument.in/reader034/viewer/2022042621/5f6bd6d52fb2b80f6460d6d8/html5/thumbnails/4.jpg)
Alex Aiken
Java Threads
class Simple {
int a = 1, b = 2;
void synchronized to() { a = 3; b = 4; }
void fro() {println("a= " + a + ", b=" + b); }
}
Two threads call to() and fro(). What is printed?
![Page 5: Introduction to Programmingopenclassroom.stanford.edu/.../18-06-java-threads... · Java Threads •Java has concurrency built in through threads –Each thread has its own program](https://reader034.vdocument.in/reader034/viewer/2022042621/5f6bd6d52fb2b80f6460d6d8/html5/thumbnails/5.jpg)
Alex Aiken
Java Threads
class Simple {
int a = 1, b = 2;
void synchronized to() { a = 3; b = 4; }
void synchronized fro() {println("a= " + a + ", b=" + b); }
}
Two threads call to() and fro(). What is printed?
![Page 6: Introduction to Programmingopenclassroom.stanford.edu/.../18-06-java-threads... · Java Threads •Java has concurrency built in through threads –Each thread has its own program](https://reader034.vdocument.in/reader034/viewer/2022042621/5f6bd6d52fb2b80f6460d6d8/html5/thumbnails/6.jpg)
Alex Aiken
Java Threads
• Even without synchronization, a variable should only hold values written by some thread
– Writes of values are atomic
– Violated for doubles, though
• Java concurrency semantics are difficult to understand in detail, particularly as to how they might be implemented on certain machines