Results 1 to 3 of 3
  1. #1
    samp is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Help with understanding Threads

    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

    Java Code:
     
    //====================================================
    
    import java.applet.Applet;
    import java.awt.Graphics;
    
    
    public class PcLab extends Applet implements Runnable {
    StringBuffer buffer;
    
    
    public void init(){ 
       buffer =  new StringBuffer();
    }
    
    
    
    public void start (){
       desktop_pc PC1 = new desktop_pc("PC1");
       desktop_pc PC2 = new desktop_pc("PC2");
    
       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 (){}
    
       public desktop_pc(String INST_NAME)
        {
          buffer = new StringBuffer();
          mythread = new Thread(this, INST_NAME);
          mythread.start();
        }  
    
    public void start(){
       buffer.append (" inside desktop_Pc, Start method");
       repaint();
    
    }
    
    public void run (){
       buffer.append (" inside desktop_Pc, run method");
       repaint();
    }
    
    
    public void super.paint(Graphics g){
    g.drawRect(0,0,getWidth()-1, getHeight()-1);
    g.drawString(buffer.toString(),30,150);
    
    } 
    
    
    }
    
    //========================
    
    file : main.java
    
     <applet code=PcLab.class width=900 height=200>
        <param name=fps value= 360>
        </applet>
    
    //=============

  2. #2
    samp is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Hi,
    in my previous code, I had mentioned "super.paint()" . My original code does not contain "super.paint()". it is only just paint().

  3. #3
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    PcLab is the only applet in the html file, so that is the only one rendered on the browser.
    No matter what you do in the desktop_pc class, you won't see anything.

    desktop_pc need not be an applet. In fact, applets are meant to be rendered by an HTML page.
    So

    new desktop_pc("PC1");
    is meaningless.

    Make desktop_pc a simple runnable class. Add a public function
    in PcLab to allow it's buffer to be accessed from desktop_pc.

    Create threads inside PcLab using two instances of desktop_pc,
    and mak desktop_pc append PcLab's buffer...

    See this:
    Defining and Starting a Thread (The Java™ Tutorials > Essential Classes > Concurrency)
    Last edited by cultclassic; 03-09-2011 at 07:49 PM.

Similar Threads

  1. Am I understanding this correctly?
    By phixion in forum New To Java
    Replies: 9
    Last Post: 01-27-2011, 06:13 AM
  2. Understanding this recursion
    By Yakg in forum New To Java
    Replies: 6
    Last Post: 01-05-2011, 10:39 PM
  3. Vector understanding
    By counterfox in forum New To Java
    Replies: 6
    Last Post: 05-04-2010, 11:59 AM
  4. Help on understanding a program
    By newbie225 in forum New To Java
    Replies: 1
    Last Post: 11-10-2009, 01:53 AM
  5. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 07:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •