PROGRAMA DE MEMORIA DINAMICA
INTRODUCCIÓNLa memoria dinámica sirve para que los programas se adapten siempre al tamaño del problema que tienen que resolver, sin desperdiciar recursos de memoria. Esto se traduce asimismo en una mayor eficiencia en la ejecución de los programas.La memoria dinámica es un espacio de almacenamiento que se puede solicitar en tiempo de ejecución. Además de solicitar espacios de almacenamiento, también podemos liberarlos (en tiempo de ejecución) cuando dejemos de necesitarlos.
OBJETIVOCrear un programa de simulación de procesos, utilizando memoria dinámica.
CODIGO FUENTE
import java.awt.*;import javax.swing.*;import java.util.*;import java.awt.event.*;import java.awt.*;import java.applet.*;import java.util.Random;
public class ParticleApplet extends Applet {
protected Thread[] threads=null;protected final ParticleCanvas canvas = new ParticleCanvas(300);public int cont=0;public int segmentos[];public int resp_segmentos[]=new int[20];public int valor_total=60;public int valor_resta=0;
public void init() {
add(canvas);}
public void paint(Graphics g) { g.setColor(Color.black); g.fillRect(0,0,800,800);
}protected Thread makeThread(final Particle p){
Runnable runloop=new Runnable()
{public void run(){System.out.println("runloop valor= "+p.valor+"de: "+p.nombre);
try{
System.out.println("runloop volumen= "+p.volumen);for(;;){
if(p.valor==0){
System.out.println("runloop volumen= "+p.volumen);
p.move();
canvas.repaint();
Thread.sleep(1300);}
}}catch(InterruptedException e){return;}
}};return new Thread(runloop);
}
public synchronized void start(){
int n=15;int v=15;int x1=15;int y2=101;int e0;int div=0;int p=0;int e;
if(threads==null){
Particle[] particles= new Particle[n];JOptionPane.showMessageDialog(null, "PROGRAMA DE
ADMINISTRACÌÓN DE MEMORIA","MEMORIA",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "EL MAXIMO DE MEMORIA ES DE 60 MB\n POR FAVOR PIENSE BIEN EL TAMAÑO DEL PROCESO","MEMORIA",JOptionPane.INFORMATION_MESSAGE);
String respc1;respc1 = JOptionPane.showInputDialog("1)DAR NUMERO DE
PARTICIONES");e0 = Integer.parseInt(respc1);
/*if(e0==1){
JOptionPane.showMessageDialog(null, "EL MAXIMO DE particiones es 5","MEMORIA",JOptionPane.INFORMATION_MESSAGE);
do{
respc1 = JOptionPane.showInputDialog("1)CUANTO MIDE LA PARTICION?");
e0 = Integer.parseInt(respc1);resp_segmentos[p]=e0;p+=1;valor_total=valor_total-e0;valor_resta+=valor_total;System.out.println("valor_resta= "+valor_resta);System.out.println("valor_total= "+valor_total);System.out.println("p= "+p);
}while(valor_total!=0);}*/
if(e0==1){
JOptionPane.showMessageDialog(null, "EL MAXIMO DE particiones es 4","MEMORIA",JOptionPane.INFORMATION_MESSAGE);
respc1 = JOptionPane.showInputDialog("1)CUANTAS PARTICIONES QUIERE?");
e0 = Integer.parseInt(respc1);JOptionPane.showMessageDialog(null, "LA DIRECCION
QUE ELIJA DEBE EMPEZAR ADELANTE DE LOS 300","MEMORIA",JOptionPane.INFORMATION_MESSAGE);
String respc4 = JOptionPane.showInputDialog("EN QUE DIRECCION DESEA EMPEZAR?");
int e4 = Integer.parseInt(respc1);
div=64/e0;System.out.println("div= "+div);
if(e0==2){
do{System.out.println("Programa MEMORIA");String respc;
respc = JOptionPane.showInputDialog("1)Nuevo PROCESO \n2) VER PROCESOS");
e = Integer.parseInt(respc);if(e==1){
respc = JOptionPane.showInputDialog(" ¿ Cantidad de TAMAÑO ? ");
int c = Integer.parseInt(respc);if(c<31){
cont+=1;if((cont%5)==0){
y2+=200;x1=5;
}if(cont==1)
particles[cont-1]=new Particle("PROCESO "+cont,x1,y2,16,16,false,0,c,2);
elseparticles[cont-1]=new
Particle("PROCESO "+cont,x1,y2,16,16,false,v,c,2);v-=1;
y2+=73;}else
JOptionPane.showMessageDialog(null, "EL MAXIMO DE TAMAÑO ES 30","MEMORIA",JOptionPane.INFORMATION_MESSAGE);
}}while(e!=2);}
else if(e0==3)
{do{System.out.println("Programa MEMORIA");String respc;
respc = JOptionPane.showInputDialog("1)Nuevo PROCESO \n2) VER PROCESOS");
e = Integer.parseInt(respc);if(e==1){
respc = JOptionPane.showInputDialog(" ¿ Cantidad de TAMAÑO ? ");
int c = Integer.parseInt(respc);if(c<21){
cont+=1;if((cont%5)==0){
y2+=200;x1=5;
}if(cont==1)
particles[cont-1]=new Particle("PROCESO "+cont,x1,y2,16,16,false,0,c,3);
elseparticles[cont-1]=new
Particle("PROCESO "+cont,x1,y2,16,16,false,v,c,3);v-=1;
y2+=73;}else
JOptionPane.showMessageDialog(null, "EL MAXIMO DE TAMAÑO ES 20","MEMORIA",JOptionPane.INFORMATION_MESSAGE);
}}while(e!=2);}else if(e0==4){do{System.out.println("Programa MEMORIA");String respc;
respc = JOptionPane.showInputDialog("1)Nuevo PROCESO \n2) VER PROCESOS");
e = Integer.parseInt(respc);if(e==1){
respc = JOptionPane.showInputDialog(" ¿ Cantidad de TAMAÑO ? ");
int c = Integer.parseInt(respc);if(c<15){
cont+=1;if((cont%5)==0){
y2+=200;x1=5;
}if(cont==1)
particles[cont-1]=new Particle("PROCESO "+cont,x1,y2,16,22,false,0,c,4);
elseparticles[cont-1]=new
Particle("PROCESO "+cont,x1,y2,16,22,false,v,c,4);v-=1;
y2+=73;}else
JOptionPane.showMessageDialog(null, "EL MAXIMO DE TAMAÑO ES 15","MEMORIA",JOptionPane.INFORMATION_MESSAGE);
}}while(e!=2);}
}/*resp_segmentos[p]=e0;p+=1;*/
canvas.setParticles(particles);threads=new Thread[cont];
for(int i=0;i<cont;++i){
System.out.println("numero de hilos "+threads.length);threads[i]=makeThread(particles[i]);
threads[i].setPriority(i+1);threads[i].start();
}
}
}
public synchronized void stop(){if(threads != null){
for(int i=0;i<threads.length;++i)threads[i].interrupt();
threads=null;}
}
}
EJECUCIÓN DEL PROGRAMA
Primero nos pide el numero de particiones:
Nos pide la dirección donde deseamos empezar
Después tenemos la opción de crear un nuevo proceso
Damos el tamaño del proceso
Top Related