Results 1 to 3 of 3
  1. #1
    gunitinug is offline Member
    Join Date
    Dec 2016
    Posts
    13
    Rep Power
    0

    Default Basic Thread to increment a global variable

    Java Code:
    public class Test {
    	static int shared=0;
    	static Thread t1;
    	static Thread t2;
    	
    	public static void main(String[] args) {
    		t1=new Thread(new Runnable() {
    			
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				++shared;
    			}
    		});
    		
    		t2 = new Thread(new Runnable() {
    			
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				--shared;
    			}
    		});
    		
    		t1.start(); // expect shared to be incremented to 1.
    		System.out.println(shared); // shared is still 0.
    		
    		
    	}
    	
    	
    }
    Value of shared is still 0 when program is run, when I expected shared to be incremented to equal to 1. How to let thread t1 to increment shared to 1?

  2. #2
    gunitinug is offline Member
    Join Date
    Dec 2016
    Posts
    13
    Rep Power
    0

    Default Re: Basic Thread to increment a global variable

    Java Code:
    public class Test {
    	static int shared=0;
    	static Thread t1;
    	static Thread t2;
    	
    	public static void main(String[] args) {
    		t1=new Thread(new Runnable() {
    			
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				++shared;
    			}
    		});
    		
    		t2 = new Thread(new Runnable() {
    			
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				++shared;
    			}
    		});
    		
    		
    		try {
    			t1.start();
    			t1.join();
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		System.out.println(shared);
    		
    		
    		try {
    			t2.start();
    			t2.join();
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		System.out.println(shared);
    		
    	}
    	
    	
    }
    This works. I had to call thread.join(). Also, the same java thread can't be started more than once.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Basic Thread to increment a global variable

    Quote Originally Posted by gunitinug View Post
    This works. I had to call thread.join().
    Uh, no. That may be how you got it to work but you didn't have to call Thread.join().
    You simply didn't give the thread enough time to start before printing out the value.
    In your original code, after you start the thread, put this after it.
    Java Code:
    try  {
         Thread.sleep(2000);
    } catch (InterruptedException ie) {}
    Also, the same java thread can't be started more than once.
    True. But I'm not certain what that has to do with anything since in your first post you
    didn't try to restart a thread.

    And you might want to stay away from static values when trying to understand thread
    behavior. Static variables are "shared" across all instances of a class whether you are using
    threads or not.

    Regards,
    Jim
    Last edited by jim829; 12-04-2016 at 10:15 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Why won't this variable increment!
    By coolman50544 in forum New To Java
    Replies: 1
    Last Post: 05-28-2014, 10:29 PM
  2. Set Global variable value
    By SteveBMill in forum Android
    Replies: 2
    Last Post: 09-18-2011, 11:48 PM
  3. How to pu a sort of Global Variable?
    By esgol in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 05:46 PM
  4. Global variable not accessible
    By bolduc4 in forum New To Java
    Replies: 3
    Last Post: 05-10-2010, 07:51 AM
  5. Increment a Variable
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 02:57 AM

Posting Permissions

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