






Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!







public class MostrarMensaje { public void mostrarM1() { for (int f = 1; f <= 100; f++) System.out.print("Proceso 1"); } public void mostrarM2() { for (int f = 1; f <= 100; f++) System.out.print("Proceso 1"); } } public class Principal { public static void main(String[] args) { MostrarMensaje m= new MostrarMensaje(); m.mostrarM1(); System.out.println(" "); m.mostrarM2(); } } public class Proceso1 extends Thread{ public Proceso1() { } @Override public void run() { for (int i=0;i<10;i++){ System.out.println("Proceso 1 "); } } } public class Proceso2 implements Runnable{ @Override public void run() { for (int i=0;i<10;i++){ System.out.println("Proceso 2 "); } } } public class Principal { public static void main(String[] args) { Proceso1 hilo1= new Proceso1(); Thread hilo2= new Thread ( new Proceso2()); //Ejecucion de los hilos hilo1.start(); hilo2.start(); }
public class MostrarCeroUno { public void mostrar0() { for (int f = 1; f <= 100; f++) System.out.print("0-"); } public void mostrar1() { for (int f = 1; f <= 100; f++) System.out.print("1-"); } } public class Principal { public static void main(String[] args) { MostrarCeroUno m= new MostrarCeroUno(); m.mostrar0(); System.out.println(" "); m.mostrar1(); } } public class HiloMostrarCero extends Thread { @Override public void run() { for (int f = 1; f <= 1000; f++) System.out.print("0-"); } } public class HiloMostrarUno extends Thread{ @Override public void run() { for (int f = 1; f <= 1000; f++) System.out.print("1-"); } } public class Prinicpal { public static void main(String[] args) { HiloMostrarCero h1 = new HiloMostrarCero(); h1.start(); HiloMostrarUno h2 = new HiloMostrarUno(); h2.start(); }
class Contador implements Runnable { private String nombre; private int retraso; public Contador(String nombre, int retraso) { this .nombre = nombre; this .retraso = retraso; } public void run() { for (int i = 1; i <= 5; i++) { System.out.println(nombre + ": " + i); try { Thread.sleep(retraso); // Pausa según el retraso } catch (InterruptedException e) { System.out.println(nombre + " interrumpido"); } } } } public class EjemploMultiplesContadores { public static void main(String[] args) { Thread hilo1 = new Thread( new Contador("Contador 1", 1000)); // 1 segundo Thread hilo2 = new Thread( new Contador("Contador 2", 2000)); // 2 segundos Thread hilo3 = new Thread( new Contador("Contador 3", 3000)); // 3 segundos hilo1.start(); hilo2.start(); hilo3.start(); }
class Suma implements Runnable { private int inicio; private int fin; private int resultado; public Suma(int inicio, int fin) { this .inicio = inicio; this .fin = fin; } public void run() { resultado = 0; for (int i = inicio; i <= fin; i++) { resultado += i; } } public int getResultado() { return resultado; } } public class EjemploSumaConcurrente { public static void main(String[] args) { Suma suma1 = new Suma(1, 50); Suma suma2 = new Suma(51, 100); Thread hilo1 = new Thread(suma1); Thread hilo2 = new Thread(suma2); hilo1.start(); hilo2.start(); try { hilo1.join(); hilo2.join(); } catch (InterruptedException e) { e.printStackTrace(); } int total = suma1.getResultado() + suma2.getResultado(); System.out.println("Suma total: " + total); }
import java.util.LinkedList ; import java.util.Queue ; class Buffer { private Queue
class HiloPrioridad implements Runnable { private String nombre; public HiloPrioridad(String nombre) { this .nombre = nombre; } public void run() { for (int i = 1; i <= 10; i++) { System.out.println(nombre + ": " + i); try { Thread.sleep(100); // Pausa de 0.1 segundos } catch (InterruptedException e) { System.out.println(nombre + " interrumpido"); } } } } public class EjemploPrioridades { public static void main(String[] args) { Thread hiloAlta = new Thread( new HiloPrioridad("Hilo Alta Prioridad")); Thread hiloBaja = new Thread( new HiloPrioridad("Hilo Baja Prioridad")); hiloAlta.setPriority(Thread.MAX_PRIORITY); // Prioridad alta hiloBaja.setPriority(Thread.MIN_PRIORITY); // Prioridad baja hiloAlta.start(); hiloBaja.start(); }
class HiloInterrumpido implements Runnable { public void run() { for (int i = 1; i <= 10; i++) { System.out.println(i); try { Thread.sleep(1000); // Pausa de 1 segundo } catch (InterruptedException e) { System.out.println("Hilo interrumpido"); return ; // Salir del método si se interrumpe } } } } public class EjemploInterrupcion { public static void main(String[] args) { Thread hilo = new Thread( new HiloInterrumpido()); hilo.start(); try { Thread.sleep(5000); // Espera 5 segundos hilo.interrupt(); // Interrumpe el hilo } catch (InterruptedException e) { e.printStackTrace(); } }
import java.awt.Dimension ; import java.awt.Image ; import java.awt.Toolkit ; import javax.swing.* ; public class MarcoCentrado extends JFrame{ public MarcoCentrado() { Toolkit miPantalla= Toolkit.getDefaultToolkit(); Dimension tamanoPantalla=miPantalla.getScreenSize(); int alturaPantalla=tamanoPantalla.height; //el alto de mi pantalla int anchoPantalla=tamanoPantalla.width; // el ancho de la pantalla this .setSize(anchoPantalla/2, alturaPantalla/2); this .setLocation(anchoPantalla/4, alturaPantalla/4); this .setTitle("MARCO CENTRADO"); // ICONO Image miIcono=miPantalla.getImage("rony.png"); setIconImage(miIcono); this .setVisible( true ); this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } import javax.swing.* ; public class MiMarco extends JFrame{ public MiMarco() { //definir el tamaño //this.setSize(300, 300); //para hacer visible el marco this .setVisible( true ); //para que se cierre el marco this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //titulo del marco this .setTitle("Hola Ronald"); // cambiar la unicaion //this.setLocation(500, 300); //tamaño y posision this .setBounds(400, 200, 550, 550); //para que no se redimencione// el botonn de maximizar se oculte //this.setResizable(false); //pantalla completa Frame.MAXIMIZED_BOTH == //this.setExtendedState(6); }