Day 5-Java-java.util package & Multithreading
Day 5-Java-java.util package & Multithreading
Training
2016
FOUNDATION TRAINING
Java packages util and io,
overview of Multithreading 1
Deloitte Training 2016
Java
Day 5
3
Objectives of Day 5 Deloitte Training 2016
Define Serialization
4
Multithreading Deloitte Training 2016
What is … ?
Multitasking
Multiprocessing
Multithreading
5
Thread Deloitte Training 2016
Thread is the smallest unit of executable code that performs
a particular task.
An application can be divided into multiple tasks and each
task can be assigned to a thread.
Many threads executing simultaneously is termed as
Multithreading.
6
Benefits of Multithreading Deloitte Training 2016
7
Applications of thread Deloitte Training 2016
Playing Applications
sound and of displaying
thread images
simultaneously.
8
The ‘main’ thread Deloitte Training 2016
9
contd..
Two ways to create threads –
Extend the Thread class
1. Thread()
2. Thread(String threadName)
3. Thread(Runnable threadob)
4. Thread(Runnable threadob, String threadName)
5. Thread(ThreadGroup groupObj, Runnable threadob)
6. Thread(ThreadGroup groupObj, Runnable threadob, String
threadName)
Note : groupObj is the thread group to which new thread will
belong; default is the group of parent thread. If thread name is
not specified, JVM gives a name.
contd..
Execution sequence class th extends Thread
main is the calling thread {
from which all child th()
threads (thread objects) are { start() }
created or declared and
public void run()
called. { }
Threads are initialized in }
the child class constructor
by the method start().
start() invokes run(). The class samp
thread code is {
implemented in the run(). public static void main(String a[])
{ th obj1 = new th();
Reordering
th obj2 = new th();
} }
Thread States
Born: A newly created
Thread thread
States 3-1 is in a born state.
13
Different Stages in the Life of a Thread Deloitte Training 2016
New Thread
(BORN)
READY
SLEEPING SUSPENDED
RUNNING
WAITING BLOCKED
DEAD
14
Conditions that Prevent Deloitte Training 2016
Thread Execution
Thread execution is interrupted if:
Not of highest priority.
15
Managing Thread Priorities Deloitte Training 2016
18
Synchronized Block Deloitte Training 2016
19
Input-Output in Java
Java performs I/O through streams.
Stream is an abstraction that either produces information
or consumes information.
Streams are connected to physical device by Java I/O
system.
Input stream can abstract many different kinds of input –
keyboard, disk, network socket. Output stream can refer to
console, disk, network connection.
Java implements streams within class hierarchies defined in
the java.io package.
contd..
Java Streams
44
Java Packages Deloitte Training 2016
The following table lists a few built–in Java packages.
Java Package Name Description
java.lang Provides various classes, such as Object, System, and Class.
java.util Provides various classes that support collection or groups of
objects, such as hash tables, string parsing, and system properties.
java.io Defines two streams, input stream and output stream that
determine the flow of bytes from a source to destination.
java.awt Provides classes to implement graphical user interface, such as
creating buttons, check boxes, text boxes, menus, and list boxes.
java.net Provides classes that support network programming, such as
Socket, ServerSocket, and DatagramSocket.
java.applet Provides the Applet class that provides methods to display
images, play audio files, and obtain information about the applet
environment. Some of these methods are play(), getImage(),
getAppletInfo(), and getAudioClip().
45
import Statement Deloitte Training 2016
import<package_name>.<class_name>;
46
java.lang Deloitte Training 2016
The java.lang package provides various classes and interfaces that are
fundamental to Java programming.
Class Description
Object All the classes in the java.lang package are subclasses of the Object class and
inherit its methods.
Class Supports runtime processing of the class information of an object. The Class
class also supports the toString() method.
System Provides a standard interface to input, output, and error devices, such as
keyboard and Visual Display Unit (VDU).
Wrapper Represents the primitive data types, such as int, char, and double, as objects.
Character Provides methods for determining the type of character and converting
characters from uppercase to lowercase, and vice-versa.
Integer Provides methods for converting an int object to a String object.
Math Provides various numeric constants and methods for statistics, logarithms,
exponents, and trigonometry.
String Supports operations on strings and characters.
Enum Operates as the base class of all Java language enumeration types.
47
java.util Deloitte Training 2016
The java.util package provides various utility classes and interfaces that
support date/calendar operations, string manipulation, parsing, and
basic event processing.
Class Description
BitSet Creates a set of bits that grows as required.
Date Encapsulates date and time information.
Hashtable Implements a hash table, which maps keys to values.
Random Generates a stream of random numbers.
StringTokenizer Provides methods to break a string into tokens.
Scanner Scans primitive data types and strings by using regular
expressions, and then breaks then into tokens.
Arrays Provides various methods to perform various operations on
arrays, such as searching and sorting.
Calendar Provides support for date conversion and can be extended
to provide conversion for specific calendar systems.
GregorianCalendar Provides the standard calendar used worldwide. It is a
subclass of the Calendar class.
48
contd..
java.util package contains powerful classes and interfaces
which provide for extra utility.
Classes of java.util
StringTokenizer
Date
Calender
Random
Scanner
Currency
EventObject
contd..
StringTokenizer –
StringTokenizer provides the lexer (or scanner) i.e. the first step in
parsing process.
StringTokenizer implements the Enumeration. Hence, an input
string can be enumerated as the individual tokens contained in it
using StringTokenizer.
Constructors of StringTokenizer
StringTokenizer(String str)
StringTokenizer(String str, String delimiters)
StringTokenizer(String str, String delimiters, boolean delimAsToken)
Note : The processing of text often consists of parsing a
formatted input string. Parsing is the division of text into a set of
discrete parts, or tokens, which in a certain sequence can convey a
semantic meaning.
contd..
Methods of StringTokenizer –
int countTokens() – determines the number of tokens left
to be parsed and returns that number.
boolean hasMoreElements() – Returns true if one or more
tokens remain in String and false otherwise
boolean hasMoreTokens() – Returns true if one or more
tokens remain in String and false otherwise
Object nextElement() – Returns next token as an Object.
String nextToken() – Returns the next token as a String.
String nextToken(String delimiters) – Returns the next
token as a String and sets the delimiters string to that
specified by delimiters.
contd..
Date class
Encapsulates the current date and time.
Constructors of Date class
Date()
Date(long millisec)
Most of the methods are deprecated and moved to the
classes Calendar and DateFormat.
contd..
Calendar class –
An abstract class that provides for converting time in
milliseconds into a number of useful components viz..
year, month, day, hour, minute and second.
Calendar class has no constructors
Methods of Calendar class
abstract void add(int w, int v)
boolean after(Object calObj)
boolean before(Object calObj)
final void clear()
Calendar defines some int constants, which are used with
get or set components of the calendar.
contd..
Scanner class
Scanner reads formatted input and converts it into its
binary form.
Scanner can be used to read input from the console, a file,
a String, or any source that implements the Readable
interface or ReadableByteChannel.
To use Scanner, the procedure is :
1. Determine if a specific type of input is available by calling one of
Scanner’s hasNextX methods, where X is the datatype
2. If input is available, read it by calling one of Scanner’s nextX
methods.
3. Repeat the process until input is exhausted.
Scanner defines two sets of methods to read input.
The Enumeration Interface Deloitte Training 2016
55
Legacy Classes and Interfaces Deloitte Training 2016
56
java.util.regex Deloitte Training 2016
57
java.text Deloitte Training 2016
58
Summary Deloitte Training 2016
59