0

i have been trying to runout the multithread but i get the error can any one help me , i have been changing the hole lot but i havent found the way to sort out the problem: "No enclosing instance of type programa4 is accessible. Must qualify the allocation with an enclosing instance of type programa4 (e.g. x.new A() where x is an instance of programa4)." Thanks for all.

public class programa4 {

public static void main(String[] args) {

    int t=Integer.parseInt(args[0]);
    int x=1;            

    String z=args[1];

            while(x<=t){
                System.out.println("Iniciando hilo "+x);
                new hilo(z).start();
                x=x+1;
            }
}   
class hilo extends Thread{
int num;
String z;
hilo(String z){
    this.num=Integer.parseInt(z);
}

public void run() {
    int t=1;
    while(t<=num){
    System.out.println("Generando iteracion: "+ t);
    double x=Math.random()*10;
    System.out.println("Esperando "+ x +" segundos");
    try {
        Thread.sleep((long)x*1000);
        System.out.println("Iteracion terminada");


    } catch (InterruptedException e) {
        System.out.println("Se interrumpio.");
    }
    t=t+1;
    }
    System.out.println("Terminado hilo.");

}
}   
}
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • `static class hilo extends Thread` - add **static**. – OldCurmudgeon Feb 29 '16 at 17:08
  • 1
    Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – Raedwald Mar 02 '16 at 22:34

1 Answers1

2

As hilo is an inner class of programa4 there must be an isnstance of programma4 first before you can create an inner for it.

Making it static class hilo breaks that requirement.

static class hilo extends Thread {

The alternative would be to create a programa4 to attach it to.

new programa4().new hilo(z).start();
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213