HELLO THERE...
what do I have to do in order to have two Threads with each one with their different run method in order to have separete processes??
oopss...
Printable View
HELLO THERE...
what do I have to do in order to have two Threads with each one with their different run method in order to have separete processes??
oopss...
Not quite sure what you mean. :p Do you mean just create a seperate thread of execution? Either have you class extend the Thread class or have your class implement the Runnable interface and pass that class to the thread constructor. The second option is better because is frees up your class to extend another class if it so wishes.
Thanks...
what I mean.. is for example I have class 1... and I want to have two different procceses running there.. is it possible or do I have to create a class for each separate process?
Hey... Dilenger4....
let say I have a class that helps me getting the input from the user...
[code]
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
String files = new String(buff.readLine());
[/code[
and each time I create a class like this:
and when I run it... I want a new DOS window so I can type data in the different classes....Code:InputClass c=new InputClass();
you know what i mean?
Sorry i had to delete the post. For some reason it was synchronizing :mad: :p No you can do it all in the same process. Seperate classes arent need.
ohhh...
humm...could you help me with a small example???
thanks
Here is an Example..... The code just simply takes command line arguements specified as files and searches each file for certain characters. If you notice i used a static inner class to do the actual parsing. Since the inner-class implements the Runnable interface and declares a Thread we have to pass a refrence to the thread object that implements the Runnable interface which happens to be the same class.
Code:import java.io.*;
import java.util.StringTokenizer;
class Slicer{
public static void main(String[] args){
for(int i = 0; i < args.length; i++){
Test t = new Test(args[i]);
}
}
static class Test implements Runnable{
private String file;
private Thread t;
public Test(String file){
this.file = file;
t = new Thread(this);
t.start();
}
public synchronized void run(){
int c;
try{
File f = new File(file);
char[] cbuff = new char[(char)f.length()];
int index = 0;
BufferedReader buff = new BufferedReader(new FileReader(f));
while((c = buff.read(cbuff)) != -1){}
buff.close();
for(int i = 0; i < cbuff.length; i++){
switch(cbuff[i]){
case '?':
System.out.println(" Encountered a ? in file " + f.getName());
break;
case '#':
System.out.println(" Encountered a # in file " + f.getName());
break;
case '$':
System.out.println(" Encountered a $ in file " + f.getName());
break;
}
}
}catch(IOException e){System.out.println(e);}
}
}
}
There are also other issues that one has to take into account when multithreading. For a synchronized class method(static method), Java obtains an exclusive lock on the class before executing the method. For a synchronized instance method, Java
obtains an exclusive lock on the class instance. So it's actually possible(though ive never tried it)to run bolth synchronized instance methods and synchronized class methods at the same time.
Sorry for not answering before....
so.. that example seems very clear. You create a class for each Process you want. ok... I have two questions :)
1. What about if I want different code in the run method for the thread... (I can of know the answer.... USE a if statement or declaring different kind of classes with different run methods?
2. I didnt understand quite weel what the synchronized word was for. I am going to do some research but if you can help me a little I would cretanly appreciate it.
THANK you
:cool: