21-Threads - 4/14/2011 Computer Processor Trends CS/ENGRD...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
4/14/2011 1 CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2010 Thorsten Joachims Lecture 21: Threads and Concurrency Computer Processor Trends Moore ’s Law : Computer speeds and memory densities nearly double each year Multicore: use additional transistors to put more CPUs (cores) on one chip. 2 Concurrency (aka Multitasking) Multiple processes Multiple independently running programs Multiple threads Same program has multiple streams of execution Special problems arise race conditions deadlock 3 What is a Thread? A separate process that can perform a computational task independently and concurrently with other threads Most programs have only one thread GUIs have a separate thread, the event dispatching thread A program can have many threads You can create new threads in Java 4 What is a Thread? # Threads # Processors # Cores The processor cores distributes their time over all the active threads Implemented with support from underlying operating system or virtual machine Gives the illusion of many threads running simultaneously, even if more threads than processors / cores 5 Threads in Java Threads are instances of the class Thread can create as many as you like The Java Virtual Machine permits multiple concurrent threads initially only one thread (executes main) Threads have a priority higher priority threads are executed preferentially a newly created Thread has initial priority equal to the thread that created it (but can change) 6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4/14/2011 2 Creating a new Thread (Method 1) 7 class PrimeThread extends Thread { long a, b; PrimeThread(long a, long b) { this.a = a; this.b = b; } public void run() { //compute primes between a and b ... } } PrimeThread p = new PrimeThread(143, 195); p.start(); overrides Thread.run() can call run() directly – the calling thread will run it or, can call start() – will run run() in new thread Creating a new Thread (Method 2) 8 class PrimeRun implements Runnable { long a, b; PrimeRun(long a, long b) { this.a = a; this.b = b; } public void run() { //compute primes between a and b ... } } PrimeRun p = new PrimeRun(143, 195); new Thread(p).start(); Example 9 Thread[Thread-0,5,main] 0 Thread[main,5,main] 0 Thread[main,5,main] 1 Thread[main,5,main] 2 Thread[main,5,main] 3 Thread[main,5,main] 4 Thread[main,5,main] 5 Thread[main,5,main] 6 Thread[main,5,main] 7 Thread[main,5,main] 8 Thread[main,5,main] 9 Thread[Thread-0,5,main] 1 Thread[Thread-0,5,main] 2 Thread[Thread-0,5,main] 3 Thread[Thread-0,5,main] 4 Thread[Thread-0,5,main] 5 Thread[Thread-0,5,main] 6 Thread[Thread-0,5,main] 7 Thread[Thread-0,5,main] 8 Thread[Thread-0,5,main] 9 public class ThreadTest extends Thread { public static void main(String[] args) { new ThreadTest().start(); for (int i = 0; i < 10; i++) { System. out .format("%s %d\n", Thread. currentThread (), i); } } public void run() { for (int i = 0; i < 10; i++) { System. out
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/17/2011 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell University (Engineering School).

Page1 / 5

21-Threads - 4/14/2011 Computer Processor Trends CS/ENGRD...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online