Java Programming-Unit 3
Java Programming-Unit 3
IVth Semester
CIC-212
autoboxing, now co
mpiler will write Inte
ger.valueOf(a) intern
ally
1. toUpperCase()
2. toLowerCase()
3. trim() -> eliminates white spaces before and after string
4. startsWith() -> Boolean
5. endsWith() -> Boolean
6. charAt() -> returns a character at specified index
7. length()
8. replace() -> replaces all occurrence of first sequence of character with
second sequence of character
1. Newborn State: When a thread object is created a new thread is born and
said to be in Newborn state.
2. Runnable State: If a thread is in this state it means that the thread is ready
for execution and waiting for the availability of the processor. If all threads in
queue are of same priority then they are given time slots for execution in
round robin fashion.
3. Running State: It means that the processor has given its time to the thread
for execution. A thread keeps running until the following conditions occurs.
1. Thread give up its control on its own and it can happen in the following situations.
1. A thread gets suspended using suspend() method which can only be revived with resume()
method
2. A thread is made to sleep for a specified period of time using sleep(time) method, where
time in milliseconds
3. A thread is made to wait for some event to occur using wait () method. In this case a thread
can be scheduled to run again using notify () method.
2. A thread is pre-empted by a higher priority thread
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Thread States(contd..)
1. Yield Method - Causes the currently running thread to yield to any other
threads of the same priority that are waiting to be scheduled.
2. Stop Method - kills the thread on execution
3. Sleep Method - Causes the currently running thread to block for at least
the specified number of milliseconds. You need to handle exception while
using sleep() method.
4. Suspend and Resume Method - A suspended thread can be revived by
using the resume() method. This approach is useful when we want to suspend
a thread for some time due to certain reason but do not want to kill it.
Example
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Thread Priority
Every Java thread has a priority that helps the operating system determine
the order in which threads are scheduled
Java priorities are in the range between MIN_PRIORITY (a constant of 1)
and MAX_PRIORITY (a constant of 10). By default, every thread is given
priority NORM_PRIORITY (a constant of 5).
Threads with higher priority are more important to a program and should
be allocated processor time before lower-priority threads. However,
thread priorities cannot guarantee the order in which threads execute and
very much platform dependent
Example
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Use of isAlive() and join() method
1. When two or more threads need access to a shared resource, they need some
way to ensure that the resource will be used by only one thread at a time. The
process by which this synchronization is achieved is called thread
synchronization.
2. The synchronized keyword in Java creates a block of code referred to as a
critical section. Every Java object with a critical section of code gets a lock
associated with the object. To enter a critical section, a thread needs to obtain
the corresponding object's lock.
Example
1. In the example method update() is not synchronized and access by both
the threads simultaneously which results in inconsistent output.
2. Making a method synchronized, Java creates a “monitor” and hands it over
to the thread that calls the method first time. As long as the thread holds
the monitor, no other thread can enter the synchronized section of the
code. Writing the method as synchronized will make one thread enter the
method and till execution is not complete no other thread can get access
to the method.
2. TextField - The textField component allows the user to edit single line of text. When the user types a key in the text field the
event is sent to the TextField. The key event may be key pressed, Key released or key typed.
3. Button - Button is a control component that has a label and generates an event when pressed. When a button is pressed
and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button.