Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 3.99 KB  |  hits: 3  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Passing a value from activity to thread after thread is already created
  2. public void onCreate(Bundle savedInstanceState) {
  3.     super.onCreate(savedInstanceState);
  4.     setContentView(new GameSurface(this));
  5. }
  6.  
  7. @Override
  8. protected void onResume() {
  9.     super.onResume();
  10.             //Would like to pass this value
  11.             int state = 1;
  12. }
  13.  
  14. @Override
  15. protected void onPause() {
  16.     super.onPause();
  17.             //Would like to pass this value
  18.             int state = 2;
  19. }
  20.        
  21. import java.util.concurrent.atomic.AtomicInteger;
  22. /**
  23.  * @author Michael Jones
  24.  * @description Main Thread
  25.  */
  26. public class start {
  27. private AtomicInteger state;
  28. private Thread p;
  29. private Thread r;
  30. /**
  31.  * constructor
  32.  * initialize the declared threads
  33.  */
  34. public start(){
  35.     //initialize the state
  36.     this.state = new AtomicInteger(0);
  37.     //initialize the threads r and p
  38.     this.r = new Thread(new action("resume", state));
  39.     this.p = new Thread(new action("pause", state));
  40. } //close constructor
  41.  
  42. /**
  43.  * Start the threads
  44.  * @throws InterruptedException
  45.  */
  46. public void startThreads() throws InterruptedException{
  47.     if(!this.r.isAlive()){
  48.         r.start(); //start r
  49.     }
  50.     if(!this.p.isAlive()){
  51.         Thread.sleep(1000); //wait a little (wait for r to update)...
  52.         p.start(); //start p
  53.     }
  54. } //close startThreads
  55.  
  56. /**
  57.  * This method starts the main thread
  58.  * @param args
  59.  */
  60. public static void main(String[] args) {
  61.      //call the constructor of this class
  62.     start s = new start();
  63.     //try the code
  64.     try {
  65.         s.startThreads();
  66.     } catch (InterruptedException e) {
  67.         // TODO Auto-generated catch block
  68.         e.printStackTrace();
  69.     } //start the threads
  70. } //close main
  71.  
  72. } //close class start
  73.        
  74. import java.lang.Thread.State;
  75. import java.util.concurrent.atomic.AtomicInteger;
  76.  
  77. /**
  78.  * @author Michael Jones
  79.  * @description Slave Thread
  80.  */
  81. public class action implements Runnable {
  82.  
  83. private String event = "";
  84. private AtomicInteger state;
  85.  
  86. /**
  87.  * The constructor (this represents the current instance of a thread).
  88.  *
  89.  * @param event
  90.  * @param state
  91.  */
  92. public action(String event, AtomicInteger state) {
  93.     this.event = event; // update this instance of event
  94.     this.state = state; // update this instance of state
  95. } // constructor
  96.  
  97. /**
  98.  * This method will be called after YourThreadName.Start();
  99.  */
  100. @Override
  101. public void run() {
  102.     if (this.event == "resume") {
  103.         this.OnResume(); // call resume
  104.     } else {
  105.         this.OnPause(); // call pause
  106.     }
  107. } // close Runnable run() method
  108.  
  109. /**
  110.  * The resume function Use the auto lock from synchronized
  111.  */
  112. public synchronized void OnResume() {
  113.     System.out.println("[OnResume] The state was.." + this.getAtomicState()
  114.             + " // Thread: " + Thread.currentThread().getId());
  115.     this.setAtomicState(2); // change the state
  116.     System.out.println("[OnResume] The state is.." + this.getAtomicState()
  117.             + " // Thread: " + Thread.currentThread().getId());
  118. } // close function
  119.  
  120. /**
  121.  * The pause function Use the auto lock from synchronized
  122.  */
  123. public synchronized void OnPause() {
  124.     System.out.println("[OnPause] The state was.." + this.getAtomicState()
  125.             + " // Thread: " + Thread.currentThread().getId());
  126.     this.setAtomicState(1); // change the state
  127.     System.out.println("[OnPause] The state is.." + this.getAtomicState()
  128.             + " // Thread: " + Thread.currentThread().getId());
  129. } // close function
  130.  
  131. /**
  132.  * Get the atomic integer from memory
  133.  *
  134.  * @return Integer
  135.  */
  136. private Integer getAtomicState() {
  137.     return state.intValue();
  138. }// close function
  139.  
  140. /**
  141.  * Update or Create a new atomic integer
  142.  *
  143.  * @param value
  144.  */
  145. private void setAtomicState(Integer value) {
  146.     if (this.state == null) {
  147.         state = new AtomicInteger(value);
  148.     } else
  149.         state.set(value);
  150. } // close function
  151.  
  152. } // close the class
  153.        
  154. [OnResume] The state was..0 // Thread: 9
  155. [OnResume] The state is..2 // Thread: 9
  156. [OnPause] The state was..2 // Thread: 10
  157. [OnPause] The state is..1 // Thread: 10
  158.        
  159. public class Share {
  160.    public static volatile type M_shared;
  161. }