Docsity

Lecture Slides on Java Facts - Concurrent Programming | CS 361, Study notes of Computer Science

Toggle sidebar
bg1
1
page 1
CS 361 Concurrent programming
Drexel University
Fall 2004
Lecture 4
©Bruce Char and Vera Zaychik. All rights reserved by the author.
Permission is given to students enrolled in CS361 Fall 2004to
reproduce these notes for their own use.
page 2
Quiz on Tuesday April 13
• 30 minutes or less.
• Write a simple Java program with computation
and i/o (no threads).
• You should be able to do any kind of simple
program you could write in Programming II or
Data Structures in C++, but in Java.
• Be able to state information and answer simple
questions about Java compilation, classpaths,
packages.
• If you did the first example assignment easily,
there should be no problems if you can express
what you know
page 3
Things to try in Java
•Write a program that uses inheritance, interfaces,
Vectors, file I/O, packages.
•Study AWT and Java Swing to find out how to
use the Java class libraries for GUI construction.
Our Java programming won’t need it, however.
page 4
Java facts and questions
• How do you handle global variables in
Java?
• Java has automatic garbage collection -- do
new() to allocate, but never need to do
delete.
• What implications does automatic gc have
for programming?
page 5
Forcing a deallocation of storage
int big_array[100000];
……
// now we don’t want to use the storage anymore
big_array = null; // garbage collector
// automatically deallocates storage from the
// heap.
page 6
Another Java fact and question
• How do you do a linked list in Java?
pf3
pf4
pf5

Partial preview of the text

Download Lecture Slides on Java Facts - Concurrent Programming | CS 361 and more Study notes Computer Science in PDF only on Docsity!

page 1

CS 361 Concurrent programming

Drexel University

Fall 2004

Lecture 4

©Bruce Char and Vera Zaychik. All rights reserved by the author. Permission is given to students enrolled in CS361 Fall 2004 to reproduce these notes for their own use.

page 2

Quiz on Tuesday April 13

  • 30 minutes or less.
  • Write a simple Java program with computation and i/o (no threads).
  • You should be able to do any kind of simple program you could write in Programming II or Data Structures in C++, but in Java.
  • Be able to state information and answer simple questions about Java compilation, classpaths, packages.
  • If you did the first example assignment easily, there should be no problems if you can express what you know

page 3

Things to try in Java

•Write a program that uses inheritance, interfaces,

Vectors, file I/O, packages.

•Study AWT and Java Swing to find out how to

use the Java class libraries for GUI construction.

Our Java programming won’t need it, however.

page 4

Java facts and questions

  • How do you handle global variables in

Java?

  • Java has automatic garbage collection -- do

new() to allocate, but never need to do

delete.

  • What implications does automatic gc have

for programming?

Forcing a deallocation of storage

int big_array[100000]; ……

// now we don’t want to use the storage anymore big_array = null; // garbage collector // automatically deallocates storage from the // heap.

Another Java fact and question

  • How do you do a linked list in Java?

page 7

Java procedures are mainly call

by reference

  • This means that procedures can easily alter

objects passed to them as parameters.

page 8

Building your own exceptions.

  • Class BrucesArgumentNegativeException

extends Exception{};

page 9

Programming with exceptions.

class Factorial{ Public static int compute(int k) throws BrucesArgumentNegativeException, other exceptions { if (k<0) throw new BrucesArgumentNegativeException(); else return 1; }

(and later in a program) …. try { System.out.println (“answer is: “ + Factorial.compute(n)); } catch(BrucesArgumentNegativeException e) { System.err.println(e + “oops”); continue; }; ….

page 10

Java lacks some C++ features

  • No templates. But superclass is Object. Could program a stack class with Objects, and then cast into an object of another class.
  • Exception: primitive data types such as int, float are not subclasses of Object. However, there are wrapper classes Integer, Double, etc.
  • No overloading of operators (although function overloading is okay).
  • No i/o operators <<, >>.
  • No general multiple inheritance.

No environment variables in Java

(not system independent)

  • No such thing as environment variables as

there are with C/C++ and Unix because this

is not OS independent. There’s a system

property setting mechanism that accesses

properties declared in the command line

invocation called System.getProperty()…

read about it in the Java documentation.

What does “final” mean?

  • public final class FinalCircle extends

GraphicCircle { … }

  • When a class is declared with the final

modifier, it means that it cannot be

extended.

  • It’s a way of declaring constants for a class:

final int BUFFERSIZE = 10;

page 19

Thread States and Scheduling

  • A Java thread can be in new, runnable,

running, blocked, and dead states.

  • The Threads class has methods that move

the thread from one state to another.

page 20

Thread states

  • New state – a Thread newly created.
  • Runnable – after being started, the Thread can be run. It is put into the “run queue” of Threads and waits its turn to run. “Runnable” does not mean “running”.
  • Running – the thread is executing its code. On a uniprocessor machine, at most one thread can run at a time.

page 21

Thread/process states

0 1 2 3 4

stop

start (^) yield

sleep/suspend resume

stop/end run stop stop

dispatch

suspend

1 – terminated

2 – running

3 – suspended 4 - runnable page 22

More thread states

  • Blocked – the thread is waiting for something to happen It is waiting for an i/o operation it is executing to complete It has been told to sleep for a specified period of time through the sleep method It has executed the wait() method and will block until another thread executes a notify() or notifyAll() method.
  • It will return to runnable state after sleeping, notifying, etc.

More thread states

  • Terminated
    • The final state. After reaching this state the Thread can no longer execute.
    • A thread can reach this state after the run method is finished, or by something executing its stop() method.
    • Threads can kill themselves, or a thread can kill another thread.

The “run queue” of runnable

threads

  • The Java language specification does not specify how Java is supposed to choose the thread to run if there are several runnable threads of equal priority.
  • One possibility – pick a thread and run it until it completes, or until it executes a method that causes it to move into a non-running state.
  • Another possibility – “time slicing”: pick a thread and run it for a short period of time. Then, if it is not finished, suspend it and pick another thread to run for the same period of time.

page 25

The semantics of yield()

  • public static void yield()
    • Causes the currently executing thread object to temporarily pause and allow other threads to execute.
  • Java Language Reference: “In some cases, a large number of threads could be waiting on the currently running thread to finish executing before they can start executing. To make the thread scheduler switch from the current running thread to allow others to execute, call the yield() method on the current thread. In order for yield() to work, there must be at least one thread with an equal or higher priority than the current thread..” page 26

Java realities

  • Most implementations of Java typically use

time-slicing

  • In older Java versions on Solaris the default

was to use “green” threads, which meant

that there was no time-slicing. (many-to-one

thread model)

  • Currently Solaris uses “native” threads

(many-to-many thread model)

page 27

Thread synchronization

  • t.wait() blocks until t.notify() method is

called.

  • t.suspend() puts it into suspended state.
  • t.resume() makes it runnable again.

suspend and resume are deprecated

methods, do not use. A deprecated

method is one whose design has fallen out

of favor and may be unsupported in

future Java versions page 28

Thread synchronization, con’t

  • stop() is also a deprecated method, avoid its use in your programming.
  • Most of the time you should let a thread finish by having it conclude executing its run() method.
  • stop() causes an untidy conclusion to a thread, which may disrupt correct operation of other threads which may continue to run after the stopped thread.

More Thread methods

  • Thread.currentThread returns a

reference to the current Thread

  • Thread.sleep(long msecs) causes

the current thread to suspend for at least

msecs milliseconds

  • Thread.interrupt is the preferred

method for stopping a thread (not

Thread.stop)

Thread Priorities

  • With multiple threads, one can assign priorities ( a number) to each thread, and run the ones with highest priority. t.setPriority( int).
  • In some versions of Solaris multiple threads with the same priority have no time-slicing.
  • The book provides a class library that implements time slicing on Solaris. You import it and then do new Scheduler(time slice amount);
  • This course discusses techniques that avoid the use of priorities in establishing synchronization between threads.

page 37

Other situations

  • A husband and wife work in different

places. At lunch they both go visit an ATM

to withdraw money. If they withdraw

simultaneously, what would a race

condition do to their bank balance?