'Why it doesn't run the method "run"?
I have this class that it starts executing the method "parametro" but it doesn't run anything else and I can't find out why not..
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.ProcessBuilder.Redirect;
import java.io.*;
import java.nio.charset.*;
public class Proceso extends Thread{
String variable;
@Override
public void run(){
System.out.println(variable+" en run");
System.out.println("--------------------");
/*
more lines of code
*/
}
public void parametro(String comando){
this.variable = comando;
System.out.println(variable+" en parametro");
}
}
So it only prints the command in 'parametro' method, then ends...
Note: to everybody: I'm sorry for my lack of information So the missing info is that from the main class I'm calling this in this way:
// some code before
String fichero = args[0];
BufferedReader br = null;
String linea = "";
Proceso hilo = new Proceso();
try {
br = new BufferedReader(new FileReader(fichero));
while((linea = br.readLine()) != null) {
hilo.parametro(linea);
}
// some code after
Solution 1:[1]
Could it possible be because you didn't initialize a thread? Try wrapping the run method in a new thread.
new Thread(
@Override
public void run() {
*your code here*
}
}.start();
Solution 2:[2]
For the run method to do anything, you have to start the thread by initializing an object of the class and call the start() method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SatvikVejendla |
| Solution 2 |
