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.

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
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.
Computer Programming Lab 8.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 1 Introduction to Multithreading & Fork-Join Parallelism Dan Grossman Last.
CSE332: Data Abstractions Lecture 18: Introduction to Multithreading and Fork-Join Parallelism Dan Grossman Spring 2010.
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
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.
Computer Programming Lab(5).
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.
Lecture 4 Loops.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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
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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
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.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Classes - Intermediate
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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,
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
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.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Department of Computer Science
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 : “>
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
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.
LRobot Game.
An Introduction to Java – Part I, language basics
The Boolean (logical) data type boolean
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
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.
CSE373: Data Structures & Algorithms Lecture 21: Introduction to Multithreading & Fork-Join Parallelism Dan Grossman Fall 2013.
Parallelism for summation
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.
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
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()
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
6.2 for Loops Example: for ( int i = 1; i
Methods/Functions.
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

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 " + i + " says bye"); } class ex1 { public static void main(String[] args) { System.out.println("main thread start!"); for(int i=1; i <= 5; ++i) { C c = new C(i); c.start(); } System.out.println("main thread end!"); }  Thread Creation and Execution Execute the program many times and see the results are different for each time.

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 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 final int NUM_END = 10000; private static final int NUM_THREAD = 4; // assume NUM_END is divisible by NUM_THREAD public static void main(String[] args) { 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) { // insert your code here }

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 class ex4_serial { private static final int NUM_END = ; 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) { int i; if (x<=1) return false; for (i=2;i<x;i++) { if ((x%i == 0) && (i!=x)) return false; } return true; }  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