Tema 1 Introduccion
Tema 1 Introduccion
Curso 2020/21
EI1021 Grado en Ingeniería Informática
Universitat Jaume I
TEMA 1. Introducción.
● ●
● ●
●
●
●
●
●
●
●
●
●
➢
➢
➢
➢
★
★
●
●
●
●
●
●
●
●
●
●
1. Thread
// 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");
} }
}); }
}
}
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●