while suma que programa pares obtener numeros los impares for cuente con clasificar ciclo arreglo java multithreading

java - suma - Crea dos hilos, uno muestra números impares y otros pares



programa que cuente los numeros pares e impares java (19)

Estoy intentando crear dos subprocesos, un subproceso muestra incluso enteros de 0 a 10, un subproceso muestra enteros impares de 1 a 11. ¿Es el siguiente código adecuado para diseñar este programa?

public class Mythread { public static void main(String[] args) { Runnable r = new Runnable1(); Thread t = new Thread(r); t.start(); Runnable r2 = new Runnable2(); Thread t2 = new Thread(r2); t2.start(); } } class Runnable2 implements Runnable{ public void run(){ for(int i=0;i<11;i++){ if(i%2 == 1) System.out.println(i); } } } class Runnable1 implements Runnable{ public void run(){ for(int i=0;i<11;i++){ if(i%2 == 0) System.out.println(i); } } }


@aymeric answer no imprimirá los números en su orden natural, pero este código lo hará. Explicación al final.

public class Driver { static Object lock = new Object(); public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { public void run() { for (int itr = 1; itr < 51; itr = itr + 2) { synchronized (lock) { System.out.print(" " + itr); try { lock.notify(); lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }); Thread t2 = new Thread(new Runnable() { public void run() { for (int itr = 2; itr < 51; itr = itr + 2) { synchronized (lock) { System.out.print(" " + itr); try { lock.notify(); if(itr==50) break; lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }); try { t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("/nPrinting over"); } catch (Exception e) { } } }

Para lograrlo, los métodos de ejecución de los dos subprocesos anteriores deben llamarse uno después del otro, es decir, deben estar sincronizados y estoy logrando eso utilizando bloqueos.

El código funciona así: t1.run imprime el número impar y notifica a cualquier subproceso en espera que va a liberar el bloqueo, luego entra en un estado de espera.

En este punto, se invoca t2.run, imprime el siguiente número par, notifica a otros subprocesos que está a punto de liberar el bloqueo que mantiene y luego pasa al estado de espera.

Esto continúa hasta que el itr en t2.run () llega a 50, en este punto nuestro objetivo se ha alcanzado y tenemos que matar estos dos hilos.

Al interrumpir, evito llamar a lock.wait () en t2.run y, por lo tanto, el subproceso t2 se cierra, el control ahora irá a t1.run ya que estaba esperando para adquirir el bloqueo; pero aquí su valor será> 51 y saldremos de su ejecución (), cerrando así el hilo.

Si no se usa break en t2.run (), aunque veremos los números del 1 al 50 en la pantalla, los dos subprocesos entrarán en una situación de interbloqueo y continuarán en estado de espera.


A continuación se muestra el código que utiliza el bloqueo en un objeto compartido que tiene el número que se debe imprimir. Garantiza la secuencia también a diferencia de la solución anterior.

public class MultiThreadPrintNumber { int i = 1; public synchronized void printNumber(String threadNm) throws InterruptedException{ if(threadNm.equals("t1")){ if(i%2 == 1){ System.out.println(Thread.currentThread().getName()+"--"+ i++); notify(); } else { wait(); } } else if(threadNm.equals("t2")){ if(i%2 == 0){ System.out.println(Thread.currentThread().getName()+"--"+ i++); notify(); } else { wait(); } } } public static void main(String[] args) { final MultiThreadPrintNumber obj = new MultiThreadPrintNumber(); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { while(obj.i <= 10){ obj.printNumber(Thread.currentThread().getName()); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("done t1"); } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { try { while(obj.i <=10){ obj.printNumber(Thread.currentThread().getName()); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("done t2"); } }); t1.setName("t1"); t2.setName("t2"); t1.start(); t2.start(); } }

La salida se verá como: t1--1 t2--2 t1--3 t2--4 t1--5 t2--6 t1--7 t2--8 t1--9 t2--10 hecho t2 hecho t1


Casi todo lo que es necesario si se le pide que imprima números impares de manera sincronizada.

public class ThreadingOddEvenNumbers { void main(String[] args) throws InterruptedException { Printer printer = new Printer(57); Thread t1 = new Thread(new MyRunner(printer, true), "EvenPrinter"); Thread t2 = new Thread(new MyRunner(printer, false), "OddPrinter"); t1.start(); t2.start(); t1.join(); t2.join(); } } class MyRunner implements Runnable { private Printer p; private boolean evenProperty; public MyRunner(Printer p, boolean evenNess) { this.p = p; evenProperty = evenNess; } public void run() { try { print(); } catch (InterruptedException ex) { System.out.println(this.getClass().getName() + " " + ex.getMessage()); } } public void print() throws InterruptedException { while (!p.isJobComplete()) { synchronized (p) { if (evenProperty) while (p.isEvenPrinted()) { System.out.println("wait by: " + Thread.currentThread().getName()); p.wait(); if (p.isJobComplete()) break; } else while (!p.isEvenPrinted()) { System.out.println("wait by: " + Thread.currentThread().getName()); p.wait(); if (p.isJobComplete()) break; } } synchronized (p) { if (evenProperty) p.printEven(Thread.currentThread().getName()); else p.printOdd(Thread.currentThread().getName()); p.notifyAll(); System.out.println("notify called: by: " + Thread.currentThread().getName()); } } } } class Printer { private volatile boolean evenPrinted; private volatile boolean jobComplete; private int limit; private int counter; public Printer(int lim) { limit = lim; counter = 1; evenPrinted = true; jobComplete = false; } public void printEven(String threadName) { System.out.println(threadName + "," + counter); incrCounter(); evenPrinted = true; } public void printOdd(String threadName) { System.out.println(threadName + "," + counter); incrCounter(); evenPrinted = false; } private void incrCounter() { counter++; if (counter >= limit) jobComplete = true; } public int getLimit() { return limit; } public boolean isEvenPrinted() { return evenPrinted; } public boolean isJobComplete() { return jobComplete; } }


No es la respuesta para el problema anterior, pero en las líneas similares.

Programa para imprimir los elementos de la matriz de forma secuencial, pero utiliza dos hilos diferentes para imprimir los elementos adyacentes

import java.util.logging.Level; import java.util.logging.Logger; /** * * @author ntv */ public class PrintLAternateNumber { public static void main(String[] args) { int [] num = {1,2,3,4,5,6}; Printer p = new Printer(); Thread t1 = new Thread(new Thread1(num, p), "Thread1"); Thread t2 = new Thread(new Thread2(num, p), "Thread2"); t1.start(); t2.start(); } } class Thread1 implements Runnable { int [] num; Printer p ; public Thread1(int[] num, Printer p) { this.num = num; this.p = p; } public void run() { try { print(); } catch (InterruptedException ex) { Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex); } } public void print() throws InterruptedException { int i = 1; while(i < num.length) { synchronized(num) { while (p.evenPrinted) { num.wait(); } } synchronized(num) { p.printEven(Thread.currentThread().getName(), num[i]); i= i + 2; num.notifyAll(); } } } } class Thread2 implements Runnable { int [] num; Printer p ; public Thread2(int[] num, Printer p) { this.num = num; this.p = p; } public void run() { try { print(); } catch (InterruptedException ex) { Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex); } } public void print() throws InterruptedException { int i = 0; while(i < num.length) { synchronized(num) { while (!p.evenPrinted) { num.wait(); } } synchronized(num) { p.printOdd(Thread.currentThread().getName(), num[i]); i = i + 2; num.notifyAll(); } } } } class Printer { boolean evenPrinted = true; void printEven(String threadName , int i) { System.out.println(threadName + "," + i); evenPrinted = true; } void printOdd(String threadName , int i) { System.out.println(threadName + "," + i); evenPrinted = false; } }


Paquete concurrente:

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; //=========== Task1 class prints odd ===== class TaskClass1 implements Runnable { private Condition condition; private Lock lock; boolean exit = false; int i; TaskClass1(Condition condition,Lock lock) { this.condition = condition; this.lock = lock; } @Override public void run() { try { lock.lock(); for(i = 1;i<11;i++) { if(i%2 == 0) { condition.signal(); condition.await(); } if(i%2 != 0) { System.out.println(Thread.currentThread().getName()+" == "+i); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { lock.unlock(); } } } //==== Task2 : prints even ======= class TaskClass2 implements Runnable { private Condition condition; private Lock lock; boolean exit = false; TaskClass2(Condition condition,Lock lock) { this.condition = condition; this.lock = lock; } @Override public void run() { int i; // TODO Auto-generated method stub try { lock.lock(); for(i = 2;i<11;i++) { if(i%2 != 0) { condition.signal(); condition.await(); } if(i%2 == 0) { System.out.println(Thread.currentThread().getName()+" == "+i); } } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { lock.unlock(); } } } public class OddEven { public static void main(String[] a) { Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); Future future1; Future future2; ExecutorService executorService = Executors.newFixedThreadPool(2); future1 = executorService.submit(new TaskClass1(condition,lock)); future2 = executorService.submit(new TaskClass2(condition,lock)); executorService.shutdown(); } }


Si esta bien Pero en este caso, no creo que necesite 2 hilos, porque la operación es simple. Sin embargo, si estás practicando hilos, está bien.


Solo cambiaría algunos detalles (no es necesario utilizar el operador de módulo aquí ...):

public class Mythread { public static void main(String[] args) { Runnable r = new Runnable1(); Thread t = new Thread(r); Runnable r2 = new Runnable2(); Thread t2 = new Thread(r2); t.start(); t2.start(); } } class Runnable2 implements Runnable{ public void run(){ for(int i=0;i<11;i+=2) { System.out.println(i); } } } class Runnable1 implements Runnable{ public void run(){ for(int i=1;i<=11;i+=2) { System.out.println(i); } } }


También me gustaría utilizar el uso simultáneo de Java si desea alternativas. Algunas de las funcionalidades proporcionadas en el paquete Java Concurrency ofrecen un nivel más alto de abstracción que el uso directo de la clase Thread y, como resultado, proporcionan más funcionalidad.

Para su caso específico, lo que está haciendo es bastante razonable, sin embargo, ¿es importante el orden de impresión de estos números? ¿Quieres emparejar antes de las probabilidades? Este tipo de preguntas indicaría mejor el diseño que mejor se adapte a sus necesidades.


clase pública ConsecutiveNumberPrint {

private static class NumberGenerator { public int MAX = 100; private volatile boolean evenNumberPrinted = true; public NumberGenerator(int max) { this.MAX = max; } public void printEvenNumber(int i) throws InterruptedException { synchronized (this) { if (evenNumberPrinted) { wait(); } System.out.println("e = /t" + i); evenNumberPrinted = !evenNumberPrinted; notify(); } } public void printOddNumber(int i) throws InterruptedException { synchronized (this) { if (!evenNumberPrinted) { wait(); } System.out.println("o = /t" + i); evenNumberPrinted = !evenNumberPrinted; notify(); } } } private static class EvenNumberGenerator implements Runnable { private NumberGenerator numberGenerator; public EvenNumberGenerator(NumberGenerator numberGenerator) { this.numberGenerator = numberGenerator; } @Override public void run() { for(int i = 2; i <= numberGenerator.MAX; i+=2) try { numberGenerator.printEvenNumber(i); } catch (InterruptedException e) { e.printStackTrace(); } } } private static class OddNumberGenerator implements Runnable { private NumberGenerator numberGenerator; public OddNumberGenerator(NumberGenerator numberGenerator) { this.numberGenerator = numberGenerator; } @Override public void run() { for(int i = 1; i <= numberGenerator.MAX; i+=2) { try { numberGenerator.printOddNumber(i); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { NumberGenerator numberGenerator = new NumberGenerator(100); EvenNumberGenerator evenNumberGenerator = new EvenNumberGenerator(numberGenerator); OddNumberGenerator oddNumberGenerator = new OddNumberGenerator(numberGenerator); new Thread(oddNumberGenerator).start(); new Thread(evenNumberGenerator).start(); }

}


paquete p.hilos;

public class PrintEvenAndOddNum { private Object obj = new Object(); private static final PrintEvenAndOddNum peon = new PrintEvenAndOddNum(); private PrintEvenAndOddNum(){} public static PrintEvenAndOddNum getInstance(){ return peon; } public void printOddNum() { for(int i=1;i<10;i++){ if(i%2 != 0){ synchronized (obj) { System.out.println(i); try { System.out.println("oddNum going into waiting state ...."); obj.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("resume...."); obj.notify(); } } } } public void printEvenNum() { for(int i=1;i<11;i++){ if(i%2 == 0){ synchronized(obj){ System.out.println(i); obj.notify(); try { System.out.println("evenNum going into waiting state ...."); obj.wait(); System.out.println("Notifying waiting thread ...."); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } }


Los números se imprimirán en secuencia

Main class =========== package com.thread; import java.util.concurrent.atomic.AtomicInteger; public class StartThread { static AtomicInteger no = new AtomicInteger(1); public static void main(String[] args) { Odd oddObj = new Odd(); Thread odd = new Thread(oddObj); Thread even = new Thread(new Even(oddObj)); odd.start(); even.start(); } } Odd Thread =========== package com.thread; public class Odd implements Runnable { @Override public void run() { while (StartThread.no.get() < 20) { synchronized (this) { System.out.println("Odd=>" + StartThread.no.get()); StartThread.no.incrementAndGet(); try { this.notify(); if(StartThread.no.get() == 20) break; this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } Even Thread =========== package com.thread; public class Even implements Runnable { Odd odd; public Even(Odd odd) { this.odd = odd; } @Override public void run() { while (StartThread.no.get() < 20) { synchronized (odd) { System.out.println("Even=>" + StartThread.no.get()); StartThread.no.incrementAndGet(); odd.notifyAll(); try { odd.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } Output (Nos are printed in sequential) ====== Odd=>1 Even=>2 Odd=>3 Even=>4 Odd=>5 Even=>6 Odd=>7 Even=>8 Odd=>9 Even=>10 Odd=>11 Even=>12 Odd=>13 Even=>14 Odd=>15 Even=>16 Odd=>17 Even=>18 Odd=>19


package com.example; public class MyClass { static int mycount=0; static Thread t; static Thread t2; public static void main(String[] arg) { t2=new Thread(new Runnable() { @Override public void run() { System.out.print(mycount++ + " even /n"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } if(mycount>25) System.exit(0); run(); } }); t=new Thread(new Runnable() { @Override public void run() { System.out.print(mycount++ + " odd /n"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } if(mycount>26) System.exit(0); run(); } }); t.start(); t2.start(); } }


package javaapplication45; public class JavaApplication45 extends Thread { public static void main(String[] args) { //even numbers Thread t1 = new Thread() { public void run() { for (int i = 1; i <= 20; i++) { if (i % 2 == 0) { System.out.println("even thread " + i); } } } }; t1.start(); //odd numbers Thread t2 = new Thread() { public void run() { for (int i = 1; i <= 20; i++) { if (i % 2 != 0) { System.out.println("odd thread " + i); } } } }; t2.start(); } }


package thread; import org.hibernate.annotations.Synchronize; class PrintOdd implements Runnable { int count = -1; private Object common; PrintOdd(Object common) { this.common = common; } @Override public void run() { synchronized (common) { while (count < 1000) { try { common.notifyAll(); System.out.println(count += 2); common.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } class PrintEven implements Runnable { int count = 0; private Object common; PrintEven(Object common) { this.common = common; } @Override public void run() { synchronized (common) { while (count < 1000) { try { common.notifyAll(); System.out.println(count += 2); common.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } public class PrintNatural { public static void main(String args[]) { Object obj = new Object(); Runnable r = new PrintOdd(obj); Thread printOdd = new Thread(r); Runnable r2 = new PrintEven(obj); Thread printEven = new Thread(r2); printOdd.start(); printEven.start(); } }


public class EvenOddNumberPrintUsingTwoThreads { public static void main(String[] args) { // TODO Auto-generated method stub Thread t1 = new Thread() { public void run() { for (int i = 0; i <= 10; i++) { if (i % 2 == 0) { System.out.println("Even : " + i); } } } }; Thread t2 = new Thread() { // int i=0; public void run() { for (int i = 0; i <= 10; i++) { if (i % 2 == 1) { System.out.println("Odd : " + i); } } } }; t1.start(); t2.start(); } }


public class MyThread { public static void main(String[] args) { // TODO Auto-generated method stub Threado o =new Threado(); o.start(); Threade e=new Threade(); e.start(); } } class Threade extends Thread{ public void run(){ for(int i=2;i<10;i=i+2) System.out.println("evens "+i); } } class Threado extends Thread{ public void run(){ for(int i=1;i<10;i=i+2) System.out.println("odds "+i); } }

SALIDA: -

cuotas 1 cuotas 3 cuotas 5 cuotas 7 cuotas 9 pares 2 pares 4 pares 6 pares 8


public class OddEvenPrinetr { private static Object printOdd = new Object(); public static void main(String[] args) { Runnable oddPrinter = new Runnable() { int count = 1; @Override public void run() { while(true){ synchronized (printOdd) { if(count >= 101){ printOdd.notify(); return; } System.out.println(count); count = count + 2; try { printOdd.notify(); printOdd.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }; Runnable evenPrinter = new Runnable() { int count = 0; @Override public void run() { while(true){ synchronized (printOdd) { printOdd.notify(); if(count >= 100){ return; } count = count + 2; System.out.println(count); printOdd.notify(); try { printOdd.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }; new Thread(oddPrinter).start(); new Thread(evenPrinter).start(); } }


public class ThreadExample { Object lock = new Object(); class ThreadEven implements Runnable { @Override public void run() { int i = 2; while (i <= 20) { synchronized (lock) { System.out.println(i + " "); i = i + 2; lock.notify(); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } class ThreadOdd implements Runnable { @Override public void run() { int i = 1; while (i <= 20) { synchronized (lock) { System.out.println(i + " "); i = i + 2; try { lock.notify(); lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } public static void main(String args[]) { ThreadExample example = new ThreadExample(); ThreadExample.ThreadOdd odd = example.new ThreadOdd(); ThreadExample.ThreadEven even = example.new ThreadEven(); Thread oT = new Thread(odd); Thread eT = new Thread(even); oT.start(); eT.start(); }


public class ThreadClass { volatile int i = 1; volatile boolean state=true; synchronized public void printOddNumbers(){ try { while (!state) { wait(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" "+i); state = false; i++; notifyAll(); } synchronized public void printEvenNumbers(){ try { while (state) { wait(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" "+i); state = true; i++; notifyAll(); } }

Entonces llama a la clase anterior como esta

// I am ttying to print 10 values. ThreadClass threadClass=new ThreadClass(); Thread t1=new Thread(){ int k=0; @Override public void run() { while (k<5) { threadClass.printOddNumbers(); k++; } } }; t1.setName("Thread1"); Thread t2=new Thread(){ int j=0; @Override public void run() { while (j<5) { threadClass.printEvenNumbers(); j++; } } }; t2.setName("Thread2"); t1.start(); t2.start();

  1. Aquí estoy tratando de imprimir los números del 1 al 10.
  2. Un hilo que intenta imprimir los números pares y otro números de hilos impares.
  3. Mi lógica es imprimir el número par después del número impar. Para este hilo de números pares debe esperar hasta que se notifique del método de números impares.
  4. Cada hilo llama a un método particular 5 veces porque estoy tratando de imprimir solo 10 valores.

fuera de puesto

System.out: Thread1 1 System.out: Thread2 2 System.out: Thread1 3 System.out: Thread2 4 System.out: Thread1 5 System.out: Thread2 6 System.out: Thread1 7 System.out: Thread2 8 System.out: Thread1 9 System.out: Thread2 10