Results 1 to 5 of 5
  1. #1
    majk is offline Member
    Join Date
    Sep 2010
    Location
    somewhere in time
    Posts
    3
    Rep Power
    0

    Default Thread problem, calling method in run method

    I have a problem and i will try to explain it as good as I can.

    public class TextForPrinting{

    ...
    ...

    public String getText(){

    return text;
    }

    private String text;
    }


    other classes are :

    public class Worker implements Runnable{

    ...
    ...

    public void run(){

    //here should be a method that prints text in TextForPrint class
    // but i'm not shore how to call a method and pass it an instance of //TextForPrint

    //something like printThis( "TextForPrint object" );
    }
    }


    public class App extends Thread{

    public static void main(String[] args){

    //now I create 12 different texts for printing
    //then

    Worker w1 = new Worker();
    Worker w2 = new Worker();
    Worker w3 = new Worker();

    new Thread(w1).start();
    new Thread(w2).start();
    new Thread(w3).start();



    }
    }


    I hope that you understand my problem. I have some coding done but because of this problem I had to change it and it didn't work so it's not worth posting

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    For you to call a method in a class you must either be in that class or have a reference to the class object.
    Your posted code doesn't show what your problem is. Can you make a program that shows the problem and gives a compile error?

    How are the Worker class and the TextForPrinting classes related?

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Norm View Post
    How are the Worker class and the TextForPrinting classes related?

    that's exactly what the op is asking. to relate the TextForPrinting with the Worker class one possibility is to create a member variable of type String inside the Worker class and create a constructor for Worker that take a string as argument. so when you create the TextForPrinting inside the App you can pass that text to the worker class. i hacked a small example

    Java Code:
    package text;
    
    class TextForPrinting {
    
        String text;
    
        public TextForPrinting(String t) {
            this.text = t;
        }
    
        public String getText() {
            return text;
        }
    }
    
    class Worker implements Runnable {
    
        private String text;
    
        public Worker(String t) {
            this.text = t;
        }
    
        public void run() {
            System.out.println("ok, printing text " + this.text);
        }
    }
    
    public class App extends Thread {
    
        public static void main(String[] args) {
    
            // now I create 3 different texts for printing
            TextForPrinting t1 = new TextForPrinting("text1");
            TextForPrinting t2 = new TextForPrinting("text2");
            TextForPrinting t3 = new TextForPrinting("text3");
    
            // here the text is passed to the threads
            new Thread(new Worker(t1.getText())).start();
            new Thread(new Worker(t2.getText())).start();
            new Thread(new Worker(t3.getText())).start();
    
        }
    }

    that produce the output

    ok, printing text text1
    ok, printing text text2
    ok, printing text text3

  4. #4
    majk is offline Member
    Join Date
    Sep 2010
    Location
    somewhere in time
    Posts
    3
    Rep Power
    0

    Default

    @j2me64 that is what I meant but it seems that I didn't explain my problem well. I'll post the text of that assignment and code that I have done.

    Printing company ZGB has a job of printing flyers, newspapers and phone bills. Every one of them contains text and printing priority. Flyer contains text of maximum length of 160 characters (without blank spaces),and printing priority 2. Phone bill has a randomly generated 7 digit number, todays date and randomly generated amount of phone bill (what customer should pay), and printing priority 1. Newspaper have printing priority 3 and they are made out of randomly generated number of flyers, except their length can't be grater then 500 characters (without blank spaces). All materials are printed on a ZGBPrinter - it just prints out text of selected category on screen and remembers how many of which category and total amount of characters are printed. Worker menages material sending to printer, and worker has a unique id number.

    Simulation should go like this :

    when simulation is started, printer is "turning on" and he waits for printing material

    4 of each category of materials for printing are prepared

    3 workers starting to send materials to printer - every 10 seconds randomly selected two of the workers are sent two of randomly selected materials for printing

    when worker gets material, he send it to printing (only one worker can print at one moment, if worker tries to print and printer is busy he should be notified when printer becomes available. Material with priority 1 has the biggest priority, then comes 2, then 3 )

    simulation ends when all materials are printed and on screen is written how many of each category of materials is printed and total amount of characters.


    Java Code:
    import java.util.*;
    
    public class App extends Thread{
    
      public static void main(String[] args){
      
        //Thread t = new Thread();
        //t.start();
        
        
        Worker rad1 = new Worker(1);
        Worker rad2 = new Worker(2);
        Worker rad3 = new Worker(3);   
        
          
        new Thread(rad1).start();
        new Thread(rad2).start();
        new Thread(rad3).start();
        
        
        
      }
    }
    Java Code:
    import java.util.*;
    
    public class Flyer extends Materijal{
    
      public Flyer(){
      
        setTekst();
        setCategory("Flyer");    
      }
      
      public void setTekst(){
      
        for(int i=0; i <16; i++){
          
          text=text + "Flyer12345 ";
          
        }
        super.setTekst(text);
      }
        
      public String getTekst(){
        
         return text; 
      }
      
      public int getPrioritet(){
      
        return prioritet;
      }
      
      
      public void setStatus(String s){
      
        this.status = s;
      }
      
      public void ispis(){
      
        super.print(text);
      }
      
      public String getStatus(){
      
        return status;
      }
      private String status = "no";
      private String category = "Flyer";
      private int prioritet=2;
      private String text="";
    }

    Java Code:
    public class Materijal{
    
      public Materijal(){}
      
      public synchronized void print(String s){
      
        System.out.println();
        System.out.println("this is text: "+s);
      }
      
      public void setCategory(String s){
      
        this.kategorija = s;
      }
      
      public String getCategory(){
      
        return kategorija;
      }
      
      public void setTekst(String t){
      
        this.text = t;
      }
      
      private String text="";
      private String kategorija= "";
    }


    Java Code:
    import java.util.*;
    
    public class Newspaper extends Materijal{
    
      public Newspaper(){
      
        Random r = new Random();
        flyerNum = r.nextInt(5)+1;
        setTekst();
        setCategory("Novine");
      }
      
      public void setTekst(){
      
        int pom = flyerNum*160;
        if(pom < 500)
          for(int i=0; i<(pom/10); i++)
          text = text + "Newspaper1234 ";
        else
          for(int j=0; j<50; j++)
          text = text + "Newspaper1234 ";
        super.setTekst(text);
      }
      
      public String getTekst(){
      
        return text;
      }
      
      public void setStatus(String s){
      
        this.status = s;
      }
      
      public String getStatus(){
      
        return status;
      }
      
      public void ispis(){
      
        super.print(text);
      }
      
      private String status = "no";
      private String kategorija = "Newspaper";
      private String text="";
      private int flyerNum;
      private int prioritet=3;
    }


    Java Code:
    import java.util.*;
    
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    
    public class Racun extends Materijal{
    
      public Racun(){
      
        setBroj();
        setDatum();
        setRacun();
        setCategory("Racun");
        setTekst();
      }
      
      public void setBroj(){
      
        Random r = new Random();
        for(int i=0; i<7; i++){
          int b = r.nextInt(8)+1;
          broj = broj+b;
        }
      }
      
      public String getBroj(){
      
        return broj;
      }
      
      private String getDateTime() {
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            Date date = new Date();
            return dateFormat.format(date);
        }
      
      public void setDatum(){
      
        datum = getDateTime();
      }
      
      public String getDatum(){
      
        return datum;
      }
      
      
      public void setRacun(){
      
        Random r = new Random();
        racun = r.nextInt(300)+1;
      }
      
      
      public int getRacun(){
      
        return racun;
      }
      
      public int getPrioritet(){
      
        return prioritet;
      }
      
      public void ispis(){
      
        super.print(text);
      }
      
      
      public void setTekst(){
      
        text += getBroj()+" "+getRacun()+" "+getDatum();
        super.setTekst(text);
      }
      
      public String getTekst(){
      
        return text;
      }
      
      public void setStatus(String s){
      
        this.status = s;
      }
      
      public String getStatus(){
      
        return status;
      }
      private String status = "nije";
      private String text = "";
      private String kategorija = "Racun";  
      private int racun;
      private String datum="";
      private String broj="";
      private int prioritet=1;
    }



    Java Code:
    import java.util.*;
    public class Stampac extends Thread{   //this is a printer class
    
      public Stampac(){}
      
      public void run(){
      
        Racun r[] = new Racun[4];  //this is phone bill
        Flyer l[] = new Flyer[4];
        Newspaper n[] = new Newspaper[4];
        
        for(int i=0; i<4; i++){
        
          r[i] = new Racun();
          l[i] = new Flyer();
          n[i] = new Newspaper();
          
        }
        
        
        //Random rand = new Random();
        //int broj = rand.nextInt(3)+1;
        //int tip = rand.nextInt(2)+1;
          
        for(int i=0; i<4; i++){
          l[i].ispis();
          l[i].setStatus("ok");
          p1++;
          brojLetaka++; // number of printed flyers
        }
        for(int i=0; i<4; i++){
          n[i].ispis();
          n[i].setStatus("ok");
          p2++;
          brojNovina++; // number of printed newspapers
        }
        for(int i=0; i<4; i++){
          r[i].ispis();
          r[i].setStatus("ok");
          p3++;
          brojRacuna++; // number of printed phone bills
        }
        
       
       broj++;
       tip++;
       
      
        
        System.out.println("Broj odstampanih novina je : "+brojNovina);
        System.out.println("Broj odstampanih letaka je : "+brojLetaka);
        System.out.println("Broj odstampanih racuna je : "+brojRacuna);
        
      }
      
      private String chk="";
      private int p1=0;
      private int p2=0;
      private int p3=0;
      private int broj=1;
      private int tip=1;
      private static int brojNovina =0;
      private static int brojLetaka=0;
      private static int brojRacuna=0;
      private String provera="";
    }


    Java Code:
    public class Worker extends Materijal implements Runnable{
    
      public Worker(int broj){
      
        setId(broj);
      }
      
      public void setId(int broj){
      
        this.id = broj;
      }
      
      public void setInfo(){
      
        info = "radnik "+id;
      }
      
      public String getInfo(){
      
        return info;
      }
      
      public void run(){
      
        try{
        
          Thread.sleep(100);
          System.out.println("Worker "+this.id+" prints");
         
          Stampac s = new Stampac();
          new Thread(s).start();
          if(s.isAlive()==true){
          
            System.out.println("Zauzet stampac!!!");
          }
        }catch(Exception e){
        
          e.printStackTrace();
        }
      }
      
      private String info="";
      private String status = "";
      private int id;
    }

    I'm sorry for the long post.
    Last edited by majk; 09-27-2010 at 01:58 PM.

  5. #5
    majk is offline Member
    Join Date
    Sep 2010
    Location
    somewhere in time
    Posts
    3
    Rep Power
    0

    Default

    That code is not good because it prints out : 3workers x 4materials = 12 of each material and it should be printed only 4 of each

Similar Threads

  1. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 12:03 PM
  2. Trigger main thread method from secondary thread?
    By DigitalMan in forum Threads and Synchronization
    Replies: 8
    Last Post: 01-26-2010, 03:13 AM
  3. Problem calling a method
    By ToeJam in forum New To Java
    Replies: 5
    Last Post: 12-12-2009, 02:45 AM
  4. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 06:30 PM
  5. Replies: 29
    Last Post: 09-25-2008, 08:55 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
  •