Académique Documents
Professionnel Documents
Culture Documents
ES-SALHI Rachida
rachida.es-salhi@ensem.ac.ma
public void stop() : cette dernière méthode permet de mettre fin, de manière
définitive, à l'exécution du thread. Une autre solution pour stopper un thread
consiste à simplement sortir de la méthode run.
Le thread d’ajout
La classe contenant main
Exemple QCM
Soit le fragment de code suivant:
1. public class Fabric extends Thread {
2. public static void main(String[] args) {
3. Thread t = new Thread(new Fabric());
4. Thread t2 = new Thread(new Fabric());
5. t.start();
6. t2.start();
7. }
8. public static void run() {
9. for(int i = 0; i < 2; i++)
10. System.out.print(Thread.currentThread().getName() + " ");
11. }
12. }
Quelle réponse est correcte?
A. La compilation échoue.
B. Aucun affichage.
C. La sortie peut être Thread-1 Thread-3 Thread-1 Thread-2
D. La sortie peut être Thread-1 Thread-3 Thread-1 Thread-3
E. La sortie peut être Thread-1 Thread-1 Thread-2 Thread-2
F. La sortie peut être Thread-1 Thread-3 Thread-3 Thread-1
G. La sortie peut être Thread-1 Thread-3 Thread-1 Thread-1
Cas non
synchronisé
Cas
synchronisé
TCP/IP ou
Machine 1 UDP Machine 2
Application 2
Application 1
Technologies d’accès :
• Sockets ou DataGram
• RMI (JAVA)
• CORBA (Multi Langages)
• EJB (J2EE)
• Web Services (HTTP+XML)
xio n
n n e
Co
write read
os is
read write
is os
ES-SALHI| Java Avancé
Exemple d’un serveur simple
import java.io.*;import java.net.*;
public class Serveur {
public static void main(String[] args) {
try {
ServerSocket ss=new ServerSocket(1234);
System.out.println("j'attend la connexion d'un client");
Socket clientSocket=ss.accept();
System.out.println ("Nouveau client connecté");
System.out.println("Génération de objet InptStream et
OutputStream de la socket");
InputStream is=clientSocket.getInputStream();
OutputStream os=clientSocket.getOutputStream();
System.out.println("J'attend un nombre (1 octet)!");
int nb=is.read();
System.out.println("J'envoie la réponse");
os.write(nb*5);
System.out.println("Déconnexion du client");
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}}} ES-SALHI| Java Avancé
Exemple d’un client simple
import java.io.*;import java.net.*;import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
System.out.println(" Créer une connexion au serveur:");
Socket clientSocket=new Socket("localhost", 123);
System.out.println("Génération de objet InptStream et OutputStream
de la socket");
InputStream is=clientSocket.getInputStream();
OutputStream os=clientSocket.getOutputStream();
System.out.print("Lire un nombre au clavier NB=");
Scanner clavier=new Scanner(System.in);
int nb=clavier.nextInt();
System.out.println("Envoyer le nombre "+nb+" au serveur");
os.write(nb);
System.out.println("Attendre la réponse du serveur:");
int rep=is.read();
System.out.println("La réponse est :"+rep);
} catch (Exception e) {
e.printStackTrace();
}}}
ES-SALHI| Java Avancé : JDBC
Recevoir et envoyer des chaînes de caractères
ClientSocket:Socket Socket
is os
Thread Thread1
os is
ClientSocket:Socket
is os
Socket
Thread os is
Thread2
ES-SALHI| Java Avancé
Implémentation d’un serveur multi-thread
Suite en TP