Java - muestra posición del arrayáis en hilos

 
Vista:
sin imagen de perfil

muestra posición del arrayáis en hilos

Publicado por aaron (1 intervención) el 05/08/2022 06:07:10
hola amigo este es mi primer pos y quisiera que me ayudara a resolver un problema que tengo de la escuela.

imagen_2022-08-04_225306709

El problema dice que tengo que crear los arreglos en main y que cuente de 0 a 500 y de 501 a 1000 y mostrarlos en dos hilo después guardarlos los resultados de los hilos en una arreglo y mostrar la posición es lo que me esta dando problemas por que todo los damas ya lo tengo solo me falta las posiciones.

[code] ----------clase main----------
public class Del_0_al_1000 {

public static void main(String[] args) {
ArrayList<ArrayList<String>> grabar = new ArrayList<ArrayList<String>>();
for (int i = 0; i < 1000; i++) {
grabar.add(new ArrayList<>());
}

hilo1 h1 = new hilo1("", grabar);
hilo2 h2 = new hilo2("", grabar);

h1.start();
h2.start();

}

public int[] llamararre1() {
int[] numeros1 = new int[501];

System.out.println("1°");
for (int i = 0; i < 501; i++) {
numeros1[i] = i;
}
return numeros1;
}
public int[] llamararre2() {
int[] numeros2 = new int[1000];
System.out.println("2°");
for (int i = 500; i < 1000; i++) {
numeros2[i] = i + 1;
}
return numeros2;
}

}

----------clase hilo1----------
public class hilo1 extends Thread {

ArrayList<ArrayList<String>> grabar = new ArrayList<ArrayList<String>>();
int numero, a;

hilo1(String llenado1, ArrayList<ArrayList<String>> a) {
super(llenado1);
grabar = a;
}

public synchronized void run() {
if (numero % 2 == 0) {
try {
Del_0_al_1000 opj = new Del_0_al_1000();
int[] vector1 = opj.llamararre1();

for (int e = 0; e < 501; e++) {

String g = String.valueOf(vector1[e]);

grabar.get(0).add(g);

int pos = grabar.indexOf(g);

System.out.println("HILO1 ▶ SE ASIGNO VALOR = " + vector1[e] + " POCISION " + pos);
}
wait();
} catch (InterruptedException ee) {
Logger.getLogger(hilo1.class.getName()).log(Level.SEVERE, null, ee);
}
}
}
}
----------clase hilo2----------
public class hilo2 extends Thread {

ArrayList<ArrayList<String>> grabar = new ArrayList<ArrayList<String>>();
int a;

hilo2(String llenado2, ArrayList<ArrayList<String>> a) {
super(llenado2);
grabar = a;
}

public synchronized void run() {
Del_0_al_1000 opj = new Del_0_al_1000();
int[] vector2 = opj.llamararre2();

notify();
for (int e = 500; e < 1000; e++) {
String g = String.valueOf(vector2[e]);

grabar.get(0).add(g);
int pos = grabar.indexOf(g);
System.out.println("\t\t\t HILO2 ▶ SE ASIGNO VALOR = "
+ vector2[e] + " POCISION " + g);

}
}

/code]
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

muestra posición del arrayáis en hilos

Publicado por Tom (1831 intervenciones) el 09/08/2022 12:51:19
Lo que no entiendo es lo del wait/notify ... no sé qué te han explicado al respecto.
Por ejemplo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class DummyArray {
	static int sources[][] = new int[2][500];
	static {
		for(int i = 0; i < 500; i++) {
			sources[0][i] = i + 1;
			sources[1][i] = 500 + i + 1;
		}
	}
	/* */
	public static void main(String args[]) throws InterruptedException {
		int dest[] = new int[1000];
		Runnable r1 = new Runnable() {
			@Override
			public void run() {
				for(int i = 0; i < 500; i++) {
					dest[i] = sources[0][i];
					System.out.printf("Thread 1 set %d into pos %d\n", dest[i], i);
				}
			}
		};
		Runnable r2 = new Runnable() {
			@Override
			public void run() {
				for(int i = 0; i < 500; i++) {
					dest[500 + i] = sources[1][i];
					System.out.printf("Thread 2 set %d into pos %d\n", dest[500 + i], 500 + i);
				}
			}
		};
		Thread thr1 = new Thread(r1);
		Thread thr2 = new Thread(r2);
 
		System.out.println("Starting threads ...");
		thr1.start();
		thr2.start();
		// Wait for finalization
		thr1.join();
		thr2.join();
		System.out.println("... Destination filled.");
	}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar