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>

Just an idea...
your run() method appends to buffer, but buffer is only initialised in start().
You start the Thread from the class constructor, so maybe there is a race condition over whether run() gets called before or after start() ?
Maybe worth trying initialising buffer when it's declared?
StringBuffer buffer =new StringBuffer();

To be precise: start() is not an interface method for Runnable. It is a Thread method. Invoking thread.start() results in spawning the thread and executing the Runnable's run() method in it.
Since you are not extending Thread (which I wouldn't recommend to do), the call at line 68 does not relate to the start() method at lines 72 and below; this code is never execuetd.

@nezachem: start() is an Applet life cycle method though. I believe that's why he has that code in start().

<applet code=PcLab.class width=900 height=200>

This is PcLab.start():

public void start (){
  // desktop_pc PC1 = new desktop_pc();
  // desktop_pc PC2 = new desktop_pc();
  
   buffer.append(" Start1 ");
   repaint();
}

This is what the OP wanders about:

I am expecting to see the string displayed on my browser window "inside desktop_pc , run method". But I dont see this happening.

(cf desctop_pc.start())

Edited 5 Years Ago by nezachem: n/a

Sorry - small error in my previous post - buffer is initialised in init(), not start(), but the same logic applies.
@nezachem: Ezzaral is right, but I too was confused for a while about Thread start() vs Applet start() in this code!

I still do not understand: how desctop_pc.start() is called in this scenario?

The applet is PcLab. The PcLab.init() creates two new desctop_pc. At this moment nobody cares that desctop_pc is an instance of Applet; it doesn't matter if is has or has not a start method. The desctop_pc() constructor immediately spawns a thread, which enters the run().

So, what is a code path to desctop_pc.start()?

I think you're correct, Nezachem. There doesn't appear to be any execution path to that start() code.

I misunderstood what you were getting at in the previous post and did not notice that he was not actually running that applet.

start() would be getting called by the Applet container if that class were the one being executed, but as you noted, desktop_pc here is just another class and start() won't execute unless explicitly called in the code.

@OP:
> So, could anybody help me understand as to what/where is my mistake I made in the code.
There are two main problems. One is that the desktop_pc code to initialize your StringBuffer in the start() method is never being called, so the run() method generates a null pointer exception when it tries to append to a null buffer reference. This can be easily corrected by moving

buffer = new StringBuffer();

into the constructor or on declaration.

That would fix the null pointer but not the second, greater problem: expecting to see changes in the desktop_pc buffer appear in your PcLab applet. The desktop_pc objects are completely separate from PcLab and their internal buffer variables are private to themselves. They do not affect PcLab at all.

If you want to write back to PcLab, you need to create a communication path to it between the two classes.

One way to do that would be having your desktop_pc constructor accept a reference to the PcLab instance and create a method such as append(String) on PcLab that would append a message to its buffer and refresh its display. The desktop_pc classes would call that method to write back to the applet.

This article has been dead for over six months. Start a new discussion instead.