• Aucun résultat trouvé

Session 1

N/A
N/A
Protected

Academic year: 2022

Partager "Session 1"

Copied!
1
0
0

Texte intégral

(1)

Lab Session 1, Concurrency and Parallelism

Jean-Viven Millo INRIA Sophia-Antipolis [email protected]

Andrea Tettamanzi UNSA

[email protected] March 7, 2014

Abstract

The goal of this session is to discover the basic mechanisms of multi-threading programming in JAVA. An Eclipse project containing the skeletons of the source files is available at the same address as this document. In order to realize the exercises, you will require the following documents i) this lab session subject ii) the skeleton of the source files iii) the course iv) the JAVADOC (http://docs.oracle.com/javase/1.4.2/docs/api/). We will mostly focus on the packagesjava.lang, java.io, andjavax.swing.

Exercise 1: The Thread class

The goal of this very first exercise is to get used to the usage of theThreadclass in Java (java.lang).

1/ Write a program where two threads print concurrently on the standard output (System.out) a different message 10000 times each.

2/ After launching the threads, the main will also print 10000 times a message.

Exercise 2: The Runnable interface

Write a program that prints a frame (javax.swing.JFrame) containing two text fields (javax.swing.JTextField) and two buttons (javax.swing.JButton). Each button is a runnable entity that is started when one clicks on it (java.awt.event.ActionListener and addActionListener()).

When started, it prints the content of the associated text field on the standard output every 500 ms (Thread.sleep()). If the text in the field changes, the printed message changes accordingly. Of course, both buttons can be activated simultaneously.

When clicking one more time on the button, it stops printing, then resumes, and so on. When the frame is closed, the two runnable entities are killed!

Exercise 3: PipedStream

Write a program where a reader thread catches the stream of characters typed on the keyboard (System.in). When a full line has been typed (the key<enter>has been pressed) (java.util.Scanner), the line is put into a pipe (java.io.PipedOutputStream).

At the other end of the pipe, another thread reads the content of the pipe (PipedInputStream) and prints it in the text area (javax.swing.JTextArea) of a frame, line by line.

Could you kill the reader when the frame is closed?

Exercise 4: Parallel bubble sort

Write a program which takes as input a huge array of numbers. This array is split intonsub-arrays andnthreads apply a bubble sort on each of thensub-arrays. Lastly, another thread merges thensorted sub-arrays into one with the same size as the original array. Of course, the resulting array should be sorted.

Compare the execution of a sequential bubble sort on an array of 40000 elements with the execution of the parallel version (as described above).

1

Références

Documents relatifs

Elle n'a pas de nom (elle est dite anonyme) et peut être déclarée dans le corps d'une autre fonction.. A part cela, elle se comporte comme une fonction classique et peut recevoir

– Cette méthode vérifie si le thread doit être mis en attente – Si oui, modification de la table, recherche d’un autre thread,. chargement du PC

Comparaison entre threads et processus Avantages et inconv´enients des threads Design d’applications multi-thread´ees Mod`eles de threads?. Threads utilisateur ou noyau, mod`eles

– WAITING : la thread est bloqué en attente d'une autre thread (cf plus loin et cours 2). – TIMED_WAITING : bloqué en attente mais l'attente est borné dans le temps (cf

-Si le thread en cours d’exécution passe la main (via yield(), est suspendu ou bloqué, l’ordonnanceur choisit dans la file d’attente prête le thread de

– Un Thread est déjà associé à la méthode main pour une application Java (ou au navigateur dans le cas d ’applettes).. (Ce Thread peut donc

Cela peut porter à confusion, mais tous les threads d'une même application sont appelés Processus dans l'environnement Windows.Ceci provient du fait que Windows désire grouper tous

- Comme nous l’avons déjà mentionné, la 2 e règle imposée par Android est de toujours passer par le thread principal pour modifier l’interface utilisateur.