0% found this document useful (0 votes)
7 views33 pages

Tema 1 Introduccion

Uploaded by

togam97561
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views33 pages

Tema 1 Introduccion

Uploaded by

togam97561
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Sistemas Distribuidos

Curso 2020/21
EI1021 Grado en Ingeniería Informática
Universitat Jaume I

TEMA 1. Introducción.
● ●
● ●

















public class MiPrograma {


1. public static void main(String[] chars) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("¿Como te llamas? ");
String nombre = br.readLine();
System.out.print("Hola " + nombre);
System.out.println(". Bienvenido a EI1021.");
}
}
2.





1. Thread

public class HiloEjemplo extends Thread {


int miId;
public HiloEjemplo(int id) { public class EjecHilosExtension {
this.miId = id; public static void main(String[] args) {
} HiloEjemplo h1 = new HiloEjemplo(1);
@Override HiloEjemplo h2 = new HiloEjemplo(2);
public void run() { HiloEjemplo h3 = new HiloEjemplo(3);
for (int j = 0; j < 10; j++) { h1.start();
System.out.print("Hilo " + miId); h2.start();
System.out.println(": " + j); h3.start();
} }
} }
} // Fin clase HiloEjemplo
2. Runnable
public class HiloEjemplo2 implements Runnable {
int miId;
public HiloEjemplo2(int id) {
this.miId = id;
}
@Override
public void run() {
for (int j = 0; j < 10; j++) { public class EjecHilosInterfaz {
System.out.print("Hilo " + miId); public static void main(String[] args) {
System.out.println(": " + j); Thread h1 = new Thread(new HiloEjemplo2(1));
} Thread h2 = new Thread(new HiloEjemplo2(2));
} Thread h3 = new Thread(new HiloEjemplo2(3));
} // Fin clase HiloEjemplo2 h1.start();
h2.start();
h3.start();
}
}
public class CalculatePrimes {
public static void main(String[] args) {
final long tStart = System.currentTimeMillis();
final long startValue = 5000000;
final long step = 5000000;
final long endValue = 45000000;
CalculatePrimes cps = new CalculatePrimes();
for(long i = startValue; i < endValue; i+= step) {
System.out.println("Desde " + i + " hasta " + (i + step) + " hay " + cps.howManyPrimes(i, i + step) +
" primos (t=" + (System.currentTimeMillis() - tStart) + ").");
}
}
public int howManyPrimes(long from, long to) {
int howMany = 0;
for (long i = from; i < to; i++) if (isPrime(i)) howMany++;
return howMany;
}
private boolean isPrime(long p) {
for (long j = 2; j <= Math.sqrt(p); j++) if (p%j == 0) return false;
return true;
}
}

public class EjecHilosExtension {
public static void main(String[] args) {
SyncHiloEjemplo h1 = new SyncHiloEjemplo(1);
SyncHiloEjemplo h2 = new SyncHiloEjemplo(2);
SyncHiloEjemplo h3 = new SyncHiloEjemplo(3);
h1.start();
h2.start();
public class SyncHiloEjemplo extends Thread {
h3.start();
int miId;
}
public SyncHiloEjemplo(int id) { this.miId = id; }
}
@Override
public void run() {
for (int j = 0; j < 10; j++) Muestra(j)
}
//
// Aquí viene la definición del método Muestra()
//
} // Fin clase SyncHiloEjemplo
// Versión static y synchronized
public static synchronized void Muestra(int miId, int valor) {
System.out.print("Hilo " + miId + ":");
System.out.println(" " + valor);
}

// Versión synchronized
public synchronized void Muestra(int valor) {
System.out.print("Hilo " + miId + ":");
System.out.println(" " + valor);
// Versión synchronized en System.out
}
public void Muestra(int valor) {
synchronized (System.out) {
System.out.print("Hilo " + miId + ":");
// Versión synchronized parcial en System.out
System.out.println(" " + valor);
public void Muestra(int valor) {
}
System.out.print("Hilo " + miId + ":");
}
synchronized (System.out) {
System.out.println(" " + valor);
}
}




public class CbkEjThreads {
public static void dameRes(int t, int r) {
System.out.println("Thread #“ + t + " devuelve: “ + r);
}
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
Thread t = new Thread(new CallbackEjemplo(i));
public class CallbackEjemplo implements Runnable {
t.start();
private int valor;
}
public CallbackEjemplo(int valor) {
System.out.println("El programa principal ha acabado.");
this.valor = valor;
}
}
}
@Override
public void run() {
int nuevo = valor;
for (int i = 2; i < 5; i++) {
nuevo += nuevo*i;
}
CbkEjThreads.dameRes(valor, nuevo);
}
}
public class EjemploSwing {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton boton = new JButton();
boton.setText("Si"); public class EscBoton implements ActionListener {
EscBoton eb = new EscBoton(); @Override
boton.addActionListener(eb); public void actionPerformed(ActionEvent e) {
frame.getContentPane().add(boton); JButton b = (JButton) e.getSource();
frame.setSize(300,300); if (b.getText().equals("Si")) b.setText("No");
frame.setVisible(true); else b.setText("Si");
} }
}); }
}
}








RFC 3986: Uniform Resource Identifier (URI): Generic Syntax:


A Uniform Resource Identifier (URI) is a compact sequence of characters that identifies an abstract or physical resource. A URI can be
further classified as a locator, a name, or both. The term “Uniform Resource Locator” (URL) refers to the subset of URIs that, in addition to
identifying a resource, provide a means of locating the resource by describing its primary access mechanism (e.g., its network “location”).






You might also like