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: global variables for threads

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default global variables for threads

    hey i have java assignment ,its like these i have 2 mythreads class which extend the original thread and main , in which class i need to put global variables for both thread and main to use,and how from thread code or main code itself i can reach them(use them as well)


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: global variables for threads

    If I understand your problem correctly, just use a synchronized getter method. When accessing the same data across multiple threads, you should make sure to use synchronized methods.
    For more information on synchronized methods, read the Oracle tutorial: Synchronized Methods (The Java™ Tutorials > Essential Classes > Concurrency)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: global variables for threads

    nope i understand what your saying,but not my problem,my teacher told us their global vars for threads ,that only them they can access,i asked how to use those global vars and how to access them she didnot know that why i am asking in here

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: global variables for threads

    To access a variable in thread you should declare it with final keyword

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: global variables for threads

    someone told me i should make private class which only threads or mains can acess them by giving the threads the right parametrs in the constructor ,and main which allocate memory for the class ,is that good sul?

Similar Threads

  1. global color histogram
    By larbie in forum Java Theory & Questions
    Replies: 6
    Last Post: February 28th, 2013, 03:13 PM
  2. How can I create a global or local script terminater?
    By YourCrazyFriend in forum Java Theory & Questions
    Replies: 5
    Last Post: March 20th, 2012, 06:33 AM
  3. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  4. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  5. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 01:42 PM