Friday, August 10, 2012

Java Threading

These are some notes from a java/c class I took 
  • Multithreading
    • if you had a program generate prime numbers and wanted to get the progress, how would this be implemented?
    • one way is with program-controlled interrupts--> stop at regular intervals
    • another ways is to interrupt with user input (user-driven interrupts)
    • one thread does i/o and other computes primes
    • simplest way to do this is by extending Thread (a class in java.lang.Thread); not the best way to do this (because you have to extend, better to use runnable--implement this)
    • you need to override the run method; start () of thread class called run()
    • the main thread calls the other thread through it's main program and they both share the cpu (it does this by calling the constructor, like new Progress()-- the constructor calls start())
    • a multithreaded program is not done until all threads are done
    • there is one other way to do this: by implementing runnable; however, you must create a thread explicitly this way
    • this means that Thread t = new MyThread() doesn't work, unless you do Thread t = new Thread(new MyThread())
    • if you put this in a constructor it would look like this: mythread = new Thread(this); //mythread is static
    • Steps to making a multithreading program:
      1. implement run or extend Thread
      2. define the run method
      3. declare control variables for stopping the thread
      4. create a constructor that calls start for the thread
      5. call the constructor from a main program or somewhere
    • Thread.currentThread() returns a reference to the executing thread
    • a thread that finishes the run method is all done and dies; you can control this by a boolean variable or setting the thread to null to have it drop out of a while loop
    • check that the other thread is finished if you don't need to stay active when that thread is done: Thread.State.TERMINATED
    • Enum<Thread.State> you can get the thread states from here
    • threading is asynchronous
  • More on threads
    • multiple threads can execute static methods (with static fields)
    • you can get the current thread name with Thread.currentThread().getName()
    • a synchronized method lets go of a lock when the thread is done calling it
    • when thread waits it lets go of lock and goes into a queue for that lock
    • the sleep function can be good for animation
    • the notify function wakes up a single arbitrarily chosen thread waiting on the object's monitor
    • notifyall wakes up all threads waiting on the object's monitor
    • notify and notifyall are defined in the Object class
    • when you call start for a thread it becomes runnable (like the ready queue for processes)
    • it goes out of the runnable queue when it blocks, sleeps, or waits
    • the sleep function does not release the lock like wait
    • note that notifiyall will place all in the runnable queue, but only one thread will get the lock
    • the problem of multithreading is that you have to worry about race conditions and data consistency
    • one of the simplest ways to implement resource sharing with threads is the producer-consumer solution, that is have a shared array where the producer fills the array and the consumer takes out
    • two threads may execute different methods of a object as long as at most one of them is synchronized
    • only a thread that holds a lock on a object may issue a wait command 

No comments:

Post a Comment