Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem while getting a number from another thread

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Problem while getting a number from another thread

    Hi,
    I have got a problem. I need get a number from another thread for example this code:
    package vyjimky;
     
    /**
     *
     * @author Lolek
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {       
        Thread1 vlak = new Thread1();
        Number bolo = new Number();
     
        vlak.k.start();
     
     
     
    }
    }
    class Thread1 implements Runnable
    {   
        private Number save = new Number();
        int i;
        Thread k;
        Vlakno (){
            k = new Thread(this,"vlakno");
        }
       public void run ()
        {
        i = 10; 
        save.setData(i);
        }
    }
    class Number
    {
    private int o;
    public synchronized void setData(int olo)
    {
        this.o=olo;
    }
     
    }
    and i want use variable "o" in method main. This code is just for example...
    Thanks


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Thread and global variable

    Hello Koren3,

    I'm abit confused as to what you require here. Could you please elaborate or give me another example?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Thread and global variable

    I am sory. I already solved this problem. But thank you for your concern.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Thread and global variable

    Quote Originally Posted by Koren3 View Post
    I am sory. I already solved this problem. But thank you for your concern.
    Well done.

    Could you please post your solution? It may help others in the future.

    Thanks!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Thread and global variable

    Yes it isīt problem. Here is code.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package vyjimky;
     
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author Lolek
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {       
            try {
     
                Thread1 vlak = new Thread1();
     
     
     
                vlak.k.start();
     
                Thread l = Thread.currentThread();
                Thread.sleep(1000);
                System.out.println(vlak.j);
     
     
     
     
            } catch (InterruptedException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
     
    }
    }
     
    class Thread1 implements Runnable
    {   
        private Number ulozeni = new Number();
        int i,j;
        Thread k;
        Vlakno (){
            k = new Thread(this,"vlakno");
        }
       public void run ()
        {
        i = 10; 
        ulozeni.setData(i);
        j=ulozeni.getData();
        }
    }
    class Number
    {
    public int i; 
    private int o;
    public synchronized void setData(int olo)
    {
        this.o=olo;
        System.out.println(o);    
     
    }
     
     
    public synchronized int getData()
    {   System.out.println(o); 
        return this.o;
    }
     
    }

Similar Threads

  1. Java Variable Conversions
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: June 23rd, 2009, 05:03 AM
  2. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM
  3. UDP server sends thread application
    By Koren3 in forum Threads
    Replies: 2
    Last Post: April 20th, 2009, 11:46 AM
  4. Replies: 24
    Last Post: April 14th, 2009, 03:43 PM
  5. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49 AM