Hello All,
I am beginner to Java and am in the process of learning.
I am currently focusing on understanding the Thread concept.
Went through various materials available in the internet, but not sure whether I understood it(thread concept) clearly.
Here is what I have written: A simple java applet called PCLab, which will be called by the browser. It instantiates two objects of another class (desktop_pc , code pasted below).
My understanding is that the instantation of the object desktop_pc (instances PC1 and PC2) will result in creation of two threads. And if this is correct, then I am expecting to see the string displayed on my browser window "inside desktop_pc , run method". But I dont see this happening.
So, could anybody help me understand as to what/where is my mistake I made in the code.
many thanks,
best regards,
sampath
//====================================================
import java.applet.Applet;
import java.awt.Graphics;
public class PcLab extends Applet implements Runnable {
StringBuffer buffer;
public void init(){
buffer = new StringBuffer();
desktop_pc PC1 = new desktop_pc();
desktop_pc PC2 = new desktop_pc();
buffer.append(" INIT ");
repaint();
}
public void start (){
// desktop_pc PC1 = new desktop_pc();
// desktop_pc PC2 = new desktop_pc();
buffer.append(" Start1 ");
repaint();
}
public void paint(Graphics g) {
g.drawString(buffer.toString(),30,30);
}
public void run() {
}
}
//============================================
file : desktop_pc.java
//================
import java.applet.Applet;
import java.awt.Graphics;
public class desktop_pc extends java.applet.Applet implements Runnable {
StringBuffer buffer;
Thread mythread;
// public desktop_pc (){
// buffer.append (" inside constructor1");
// repaint();
//}
public desktop_pc()
{
mythread = new Thread(this);
mythread.start();
}
public void start(){
buffer = new StringBuffer();
buffer.append (" inside desktop_Pc, Start method");
repaint();
}
public void run (){
buffer.append (" inside desktop_Pc, RUN1 ");
repaint();
try {
buffer.append (" inside desktop_Pc, RUN2 ");
repaint();
Thread.sleep(100);
}
catch(InterruptedException e)
{
}
}//run
public void paint(Graphics g){
//g.drawRect(0,0,getWidth()-1, getHeight()-1);
g.drawString(buffer.toString(),30,50);
} //paint
}
//====================
file : main.java
<applet code=PcLab.class width=900 height=200>
<param name=fps value= 360>
</applet>