Exercise 1 : ex1.java Thread Creation and Execution (sleep() and join()) class C extends Thread {   int i;   C(int i) { this.i = i; }   public void run()

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Advertisements

Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Multithreading.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
Lab. 2 (April 27th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
1 Apr 5, 2012 ForkJoin New in Java 7. Incrementor I package helloWorld; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask;
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
1 Java Threads Instructor: Mainak Chaudhuri
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Lecture 20: Parallelism & Concurrency CS 62 Spring 2013 Kim Bruce & Kevin Coogan CS 62 Spring 2013 Kim Bruce & Kevin Coogan Some slides based on those.
1 Object Oriented Programming Lecture XII Multithreading in Java, A few words about AWT and Swing, The composite design pattern.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Exercise 1 : ex1.java class C extends Thread { int i; C(int i) { this.i = i; } public void run() { System.out.println("Thread " + i + " says hi"); System.out.println("Thread.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
System Programming Practical Session 4: Concurrency / Safety.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Computer Science 320 A First Program in Parallel Java.
Classes - Intermediate
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Parallelism idea Example: Sum elements of a large array Idea: Have 4 threads simultaneously sum 1/4 of the array –Warning: This is an inferior first approach.
Threads in Java Two ways to start a thread
Department of Computer Science
Multithreading.
CSE 373: Data Structures & Algorithms Introduction to Parallelism and Concurrency Riley Porter Winter 2017.
Lab. 2 (April 15th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 332: Intro to Parallelism: Multithreading and Fork-Join
Lab. 3 (May 6st) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute : “>
Something about Java Introduction to Problem Solving and Programming 1.
Threads Chate Patanothai.
Lab. 2 Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment. Use cygwin.
Lab. 2 (May 12th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
Computing Adjusted Quiz Total Score
ForkJoin New in Java 7 Apr 5, 2012.
CSE373: Data Structures & Algorithms Lecture 26: Introduction to Multithreading & Fork-Join Parallelism Catie Baker Spring 2015.
null, true, and false are also reserved.
LRobot Game.
CSE332: Data Abstractions Lecture 18: Introduction to Multithreading & Fork-Join Parallelism Dan Grossman Spring 2012.
An Introduction to Java – Part I, language basics
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
Lab. 3 (May 11th) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute :
ForkJoin New in Java 7 Apr 5, 2012.
Java Based Techhnology
CSE373: Data Structures & Algorithms Lecture 21: Introduction to Multithreading & Fork-Join Parallelism Dan Grossman Fall 2013.
Units with – James tedder
Recursive GCD Demo public class Euclid {
Parallelism for summation
برنامه‌نویسی چندنخی Multi-Thread Programming
class PrintOnetoTen { public static void main(String args[]) {
Lab. 2 (May 2nd) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment. Use.
Threads in Java James Brucker.
Lab. 3 (May 1st) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute : “>
Exercise 1 : ex1.java Thread Creation and Execution
Introduction to Object-Oriented Concepts in Java
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
6.2 for Loops Example: for ( int i = 1; i
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Exercise 1 : ex1.java Thread Creation and Execution (sleep() and join()) class C extends Thread {   int i;   C(int i) { this.i = i; }   public void run() {     System.out.println("Thread " + i + " says hi");     try {       sleep(500);     } catch (InterruptedException e) {}     System.out.println("Thread " + i + " says bye");   } } class ex1 {    private static final int NUM_THREAD = 10;   public static void main(String[] args) {     System.out.println("main thread start!");     C[] c = new C[NUM_THREAD];     for(int i=0; i < NUM_THREAD; ++i) {       c[i] = new C(i);       c[i].start();     }     System.out.println("main thread calls join()!");       try {         c[i].join();       } catch (InterruptedException e) {}     System.out.println("main thread ends!");

Exercise 2 : parallel summation Write a single-threaded and multi-threaded java program that computes the sum of integer numbers 1,2,…,NUM_END where NUM_THREAD is the number of threads. For multi-threaded programming, use (i) naïve approach, and (ii) devide-and-conquer (recursive) approach. class SumThread extends Thread { int lo, hi; // fields for communicating inputs int[] arr; int ans = 0; // for communicating result SumThread(int[] a, int l, int h) { lo=l; hi=h; arr=a; } public void run() { // insert your code here class ex2 { private static int NUM_END = 10000; private static int NUM_THREAD = 4; // assume NUM_END is divisible by NUM_THREAD public static void main(String[] args) { if (args.length==2) {       NUM_THREAD = Integer.parseInt(args[0]);       NUM_END = Integer.parseInt(args[1]);   } int[] int_arr = new int [NUM_END]; int i,s; for (i=0;i<NUM_END;i++) int_arr[i]=i+1; s=sum(int_arr); System.out.println("sum=" + s) ; static int sum(int[] arr) {

Exercise 3 : parallel integration Write a single threaded and multithreaded java programs that compute following numerical integration where the variable NUM_THREAD is # of threads and the variable NUM_STEP is # of steps. See the C/MPI codes below and rewrite them with JAVA language.

Exercise 3 : parallel integration // ex3_serial.java (single threaded) // ex3.java (multithreaded)

Exercise 4 : Prime numbers Write a multithreaded java program that shows how many prime numbers between 1 and NUM_END where NUM_THREAD is the number of threads Use static load balancing approach For your programming, you can consider following single threaded code. Also, measure the execution time and compare the results class ex4_serial { private static final int NUM_END = 200000; public static void main(String[] args) { int counter=0; int i; long startTime = System.currentTimeMillis(); for (i=0;i<NUM_END;i++) { if (isPrime(i)) counter++; } long endTime = System.currentTimeMillis(); long timeDiff = endTime - startTime; System.out.println("Execution Time : "+timeDiff+"ms"); System.out.println("1..."+(NUM_END-1)+" prime# counter=" + counter +"\n"); private static boolean isPrime(int x) { if (x<=1) return false; for (i=2;i<x;i++) { if ((x%i == 0) && (i!=x)) return false; return true;