ADVANCED PROGRAMMING
Computer Programming is the art of making a computer do what you want it to do.
SINGLE AND MULTITHREADED PROCESSES
- threads are light-weight processes within a process
- ex:Multithreaded Server: For Serving Multiple Clients Concurrently
What are Threads?
A piece of code that run in concurrent with other threads.
Each thread is a statically ordered sequence of instructions.
Threads are being extensively used to express concurrency on both single and multiprocessors machines.
Programming a task having multiple threads of control – Multithreading or Multithreaded Programming.
Java has built in thread support for Multithreading
Synchronization
Thread Scheduling
Inter-Thread Communication
Threading Mechanisms...
Create a class that extends the Thread class
Create a class that implements the Runnable interface
1st method: Extending Thread class : old way (Not Good Design )
2nd method: Threads by implementing Runnable interface
Life Cycle of Thread:
- You can’ t determine sequence of running threads ,but you can change priority of threads
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:
• [Link](intNumber)
• MIN_PRIORITY = 1
• NORM_PRIORITY=5
• MAX_PRIORITY=10
Accessing Shared Resources
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:
public synchronized void update()
{
…
}
Thread concurrency/operation models
1 JAVA SERIALIZATION
want to save your data… , Want to store on disk and retrieve later.
In general: want your objects to be persistent
Approach 1 : use file IO
Write a set of methods for saving/loading each class that you care about
Coolnesses of Approach 1
Can produce arbitrary file formats
Know exactly what you want to store and get back/don’t store extraneous stuff
Can build file formats to interface w/ other codes/programs
• XML
• Tab-delimited/spreadsheet
• etc.
If your classes are nicely hierarchical, makes saving/loading simple
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.
The requirements for serialization are straightforward:
Only class instances rather than primitive types can be serialized.
For an object to be serializable, its class or some ancestor must implement the empty
Serializable interface.
An empty interface is called a marker interface.
The syntax for serialization is straightforward:
• An object is serialized by writing it to an ObjectOutputStream.
• An object is deserialized by reading it from an ObjectInputStream.
Serialization code
FileOutputStream out = new FileOutputStream( “[Link]” );
ObjectOutputStream oos = new ObjectOutputStream( out );
[Link]( new Date() );
[Link]();
Deserialization code
FileInputStream in =new FileInputStream( “[Link]” );
ObjectInputStream ois = new ObjectInputStream( in );
Date d = (Date) [Link]();
[Link]();
- Sometimes, you want to explicitly not store some non-static data
Java provides the “transient” keyword. Transient
private transient int _cachedVal=_primaryVal*2;
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!
Gotchas: #2 -- Backward compatibility
What happens if you serialize with a v 1.0 object and deserialize with a v1.1? Or vice
versa?
2 JAVA REFLECTION
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
Kinds of tasks specific to Reflection
Inspection: analyzing objects and types to gather information about their definition and
behavior.
- Find the run-time type information of an object
- Find information about a type (supertypes, interfaces, members)
o Dynamic type discovery
Manipulation: uses the information gained through inspection to change the
structure/behavior:
- create new instances of new types discovered at runtime
- dynamically invoke discovered methods
How is Reflection implemented?
Reflective capabilities need special support in language and compiler !
• Java: [Link]
• .NET: [Link]
Retrieving a Class object
[Link](): If an instance of an object is available, then the simplest way to get its
Class is to invoke [Link]().
Class c = "foo".getClass();
.class: If the type is available but there is no instance then it is possible to obtain a Class
by appending ".class" to the name of the type. This is also the easiest way to obtain the
Class for a primitive type.
boolean b;
Class c = [Link](); // compile-time error
Class c = [Link]; // correct
[Link](): If the fully-qualified name of a class is available, it is possible to get the
corresponding Class using the static method [Link]()
Class cString = [Link]("[Link];");
Inspecting a Class
After we obtain a Class object myClass, we can:
Get the class name
String s = [Link]() ;
Get the class modifiers
int m = [Link]() ;
bool isPublic = [Link](m) ;
bool isAbstract = [Link](m) ;
bool isFinal = [Link](m) ;
Test if it is an interface
bool isInterface = [Link]() ;
Get the interfaces implemented by a class
Class [] itfs = [Link]() ;
Get the superclass
Class super = [Link]() ;
3 BEANS IN JAVA
What is a Bean?
A Java Bean is a reusable software component that works with Java.
More specifically: a Java Bean is a reusable software component that can be visually
manipulated in builder tools.
Beans are Reusable Software Components.
Beans share certain common defining features.
• 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 properties allowing beans to be manipulated programatically, as well as to
support the customization mentioned above.
• Support for persistence allowing beans that have been customized in an application
builder to have their state saved and restored.
JavaBean Rules
A JavaBean must have a public, no-argument constructor (a default constructor).
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.