- Passing a value from activity to thread after thread is already created
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new GameSurface(this));
- }
- @Override
- protected void onResume() {
- super.onResume();
- //Would like to pass this value
- int state = 1;
- }
- @Override
- protected void onPause() {
- super.onPause();
- //Would like to pass this value
- int state = 2;
- }
- import java.util.concurrent.atomic.AtomicInteger;
- /**
- * @author Michael Jones
- * @description Main Thread
- */
- public class start {
- private AtomicInteger state;
- private Thread p;
- private Thread r;
- /**
- * constructor
- * initialize the declared threads
- */
- public start(){
- //initialize the state
- this.state = new AtomicInteger(0);
- //initialize the threads r and p
- this.r = new Thread(new action("resume", state));
- this.p = new Thread(new action("pause", state));
- } //close constructor
- /**
- * Start the threads
- * @throws InterruptedException
- */
- public void startThreads() throws InterruptedException{
- if(!this.r.isAlive()){
- r.start(); //start r
- }
- if(!this.p.isAlive()){
- Thread.sleep(1000); //wait a little (wait for r to update)...
- p.start(); //start p
- }
- } //close startThreads
- /**
- * This method starts the main thread
- * @param args
- */
- public static void main(String[] args) {
- //call the constructor of this class
- start s = new start();
- //try the code
- try {
- s.startThreads();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } //start the threads
- } //close main
- } //close class start
- import java.lang.Thread.State;
- import java.util.concurrent.atomic.AtomicInteger;
- /**
- * @author Michael Jones
- * @description Slave Thread
- */
- public class action implements Runnable {
- private String event = "";
- private AtomicInteger state;
- /**
- * The constructor (this represents the current instance of a thread).
- *
- * @param event
- * @param state
- */
- public action(String event, AtomicInteger state) {
- this.event = event; // update this instance of event
- this.state = state; // update this instance of state
- } // constructor
- /**
- * This method will be called after YourThreadName.Start();
- */
- @Override
- public void run() {
- if (this.event == "resume") {
- this.OnResume(); // call resume
- } else {
- this.OnPause(); // call pause
- }
- } // close Runnable run() method
- /**
- * The resume function Use the auto lock from synchronized
- */
- public synchronized void OnResume() {
- System.out.println("[OnResume] The state was.." + this.getAtomicState()
- + " // Thread: " + Thread.currentThread().getId());
- this.setAtomicState(2); // change the state
- System.out.println("[OnResume] The state is.." + this.getAtomicState()
- + " // Thread: " + Thread.currentThread().getId());
- } // close function
- /**
- * The pause function Use the auto lock from synchronized
- */
- public synchronized void OnPause() {
- System.out.println("[OnPause] The state was.." + this.getAtomicState()
- + " // Thread: " + Thread.currentThread().getId());
- this.setAtomicState(1); // change the state
- System.out.println("[OnPause] The state is.." + this.getAtomicState()
- + " // Thread: " + Thread.currentThread().getId());
- } // close function
- /**
- * Get the atomic integer from memory
- *
- * @return Integer
- */
- private Integer getAtomicState() {
- return state.intValue();
- }// close function
- /**
- * Update or Create a new atomic integer
- *
- * @param value
- */
- private void setAtomicState(Integer value) {
- if (this.state == null) {
- state = new AtomicInteger(value);
- } else
- state.set(value);
- } // close function
- } // close the class
- [OnResume] The state was..0 // Thread: 9
- [OnResume] The state is..2 // Thread: 9
- [OnPause] The state was..2 // Thread: 10
- [OnPause] The state is..1 // Thread: 10
- public class Share {
- public static volatile type M_shared;
- }