Java & Haskell
Resources: for API documentation and Concurrency Tutorial. Cut and paste from these is allowed.
https://docs.oracle.com/javase/8/docs/api/ https://docs.oracle.com/javase/tutorial/essential/concurrency/
- Look at the available methods in the Thread class. Is there a method that will let a thread sleep? How do you call it? [Beware of the exception.]
- Create a Java class that derives from the Thread class. The run method of the class should write something to the screen, sleep for a little while, and then write something to the screen again before finally returning.
- Same as previous but as a class that implements the Runnable interface.
- WriteaJavaprogramthatcreatesonethreadfromeachoftheclassesaboveandthen starts them both. The program should wait until both threads have finished before ending.
- Create a Counter class using either of the above alternatives that has a private (static) tally, a public getValue method that returns the current tally, and whose run method increments the tally 107 times.
- Run two Counter threads in parallel. What is the tally after both threads have terminated?
- Repeat 2 in Haskell. That is, create and run a Haskell thread that prints, sleeps and prints before exiting. You will need to import Control.Concurrent and for sleeping you need threadDelay.