Hi Everyone,
I have been studying a book on multi-threading and have inadvertently come accross some code that I don't really understand. I am wondering if anybody can explain to me the following:
1). What effect does using the final keyword have when instantiating objects, and
2). How is it possible to instantiate an object from an interface?
public class BothInMethod extends Object
{
private String objID;
public BothInMethod(String objID)
{
this.objID = objID;
}
public void doStuff(int val)
{
print("entering doStuff()");
int num = val * 2 + objID.length();
print("in doStuff() - local variable num=" + num);
// slow things down to make observations
try{
Thread.sleep(2000);
}catch(InterruptedException x){}
print("leaving doStuff()");
}
public void print(String msg)
{
String threadName = Thread.currentThread().getName();
System.out.println(threadName + ": " + msg);
}
public static void main(String[] args)
{
final BothInMethod bim = new BothInMethod("obj1");
Runnable runA = new Runnable()
{
public void run()
{
bim.doStuff(3);
}
};
Thread threadA = new Thread(runA, "threadA");
threadA.start();
try{
Thread.sleep(200);
}catch(InterruptedException x){}
Runnable runB = new Runnable()
{
public void run()
{
bim.doStuff(7);
}
};
Thread threadB = new Thread(runB, "threadB");
threadB.start();
}
}
If you know of any good tutorials that explain this (preferably URL's) then please let me know. Thanks heaps for your help.
Regards
Davo
I have been studying a book on multi-threading and have inadvertently come accross some code that I don't really understand. I am wondering if anybody can explain to me the following:
1). What effect does using the final keyword have when instantiating objects, and
2). How is it possible to instantiate an object from an interface?
public class BothInMethod extends Object
{
private String objID;
public BothInMethod(String objID)
{
this.objID = objID;
}
public void doStuff(int val)
{
print("entering doStuff()");
int num = val * 2 + objID.length();
print("in doStuff() - local variable num=" + num);
// slow things down to make observations
try{
Thread.sleep(2000);
}catch(InterruptedException x){}
print("leaving doStuff()");
}
public void print(String msg)
{
String threadName = Thread.currentThread().getName();
System.out.println(threadName + ": " + msg);
}
public static void main(String[] args)
{
final BothInMethod bim = new BothInMethod("obj1");
Runnable runA = new Runnable()
{
public void run()
{
bim.doStuff(3);
}
};
Thread threadA = new Thread(runA, "threadA");
threadA.start();
try{
Thread.sleep(200);
}catch(InterruptedException x){}
Runnable runB = new Runnable()
{
public void run()
{
bim.doStuff(7);
}
};
Thread threadB = new Thread(runB, "threadB");
threadB.start();
}
}
If you know of any good tutorials that explain this (preferably URL's) then please let me know. Thanks heaps for your help.
Regards
Davo