Skip to content

Instantly share code, notes, and snippets.

@mNantern
Created September 1, 2013 19:28
Show Gist options
  • Select an option

  • Save mNantern/6406682 to your computer and use it in GitHub Desktop.

Select an option

Save mNantern/6406682 to your computer and use it in GitHub Desktop.
public class ThreadSharedData {
public static void main(String[] args) throws InterruptedException{
ThreadProcess threads = new ThreadProcess();
// Construct 2 threads
Thread t1 = new Thread(threads, "t1");
Thread t2 = new Thread(threads, "t2");
//Run !
t1.start();
t2.start();
//Wait the end
t1.join();
t2.join();
//Final value
System.out.println("Final count: "+threads.count);
}
}
class ThreadProcess implements Runnable {
public int count = 0;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
count++;
System.out.println("Thread:" +Thread.currentThread().getName()+ " Count:"+count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment