(p1) Advanced Programming
(p1) Advanced Programming
Computer Programming is the art of making a computer do what you want it to do.
Threads are being extensively used to express concurrency on both single and multiprocessors machines.
Synchronization
Thread Scheduling
Inter-Thread Communication
Threading Mechanisms...
Thread Priority
In Java, each thread is assigned priority, which affects the order in which it is scheduled for running.
The threads so far had same default priority (NORM_PRIORITY) and they are served using FCFS
policy.
• Java allows users to change priority:
• ThreadName.setPriority(intNumber)
• MIN_PRIORITY = 1
• NORM_PRIORITY=5
• MAX_PRIORITY=10
If one thread tries to read the data and other thread tries to update the same data, it leads to
inconsistent state.
This can be prevented by synchronising access to the data.
Use “Synchronized” method:
Write a set of methods for saving/loading each class that you care about
Coolnesses of Approach 1
Know exactly what you want to store and get back/don’t store extraneous stuff
• XML
• Tab-delimited/spreadsheet
• etc.
Pains of Approach 1
Painful when dealing with complex objects (needs recursive processing.
Serialization is the process of transforming an in-memory object to a byte stream.
Deserialization is the inverse process of reconstructing an object from a byte stream to
the same state in which the object was previously serialized.
“Serializing out” and “serializing in” are also used.
oos.close();
Deserialization code
FileInputStream in =new FileInputStream( “save.ser” );
ois.close();
Gotchas: #1 – Efficiency
For tables , it is not necessarily efficient, and may even be wrong bec By default, Java
will store the entire internal _table, including all of its null entries!
What is Reflection
Reflection: the process by which a program can observe and modify its own structure
and behavior at runtime.
Based on RTTI (Run-Time Type Identification):
RTTI: allows programs to discover at runtime and use at runtime types that were not known at
their compile time
3 BEANS IN JAVA
What is a Bean?
More specifically: a Java Bean is a reusable software component that can be visually
manipulated in builder tools.
• Support for introspection allowing a builder tool to analyze how a bean works.
• Support for customization allowing a user to alter the appearance and behavior of a
bean.
• Support for events allowing beans to fire events, and informing builder tools about both
the events they can fire and the events they can handle.
• Support for persistence allowing beans that have been customized in an application
builder to have their state saved and restored.
JavaBean Rules
The JavaBean class attributes must be accessed via accessor and mutator methods that follow a
standard naming convention (getXxxx and setXxxx, isXxxx for boolean attributes. This allows
frameworks to automate operations on attribute values.
The JavaBean class should be serializable. This allows Java applications and frameworks to save,
store, and restore the JavaBean’s state.
4 THE JAVA™ PLATFORM
Java comes in many flavours to suit the required application and complexity of hardware.