1. Threads
are subtasks in a program that can be in execution concurrently.
2. The
concurrency that computers perform today is normally implemented as operating
system primitives” available only to highly experienced “systems programmers.”
3. Java
makes concurrency primitives available to the programmer.
4. Subclass
the Thread class
and override its run(
) method.
5. implement
the Runnable interface
and pass the Runnable object
to the Thread constructor.
Runnable has one method run().
6. Signature of function: public
void run( )
7. run
method may invoke other methods; it may construct other objects; it may even
spawn other threads. The thread starts here and it stops here. When the run( ) method
completes, the thread dies.
8. the
run( ) method
is to a thread what the main(
)method is to a traditional nonthreaded program.
9. A
single-threaded program exits when the main( ) method returns.
10. A
multithreaded program exits when both the main( ) method and the run( ) methods of all
nondaemon threads return. (Daemon threads perform background tasks such as
garbage collection and don't prevent the virtual machine from exiting.)
11. Extending
Thread Class
class
A extends Thread
{
public
static void main (String[] args)
{
Thread
t = new A();
t.start(
);
}
public
void run()
{
//details
}
}
12. Implementing
Runnable:
class
A implements Runnable
{
public
static void main (String[] args)
{
A
a = new A();
Thread
t = new Thread(a);
t.start(
);
}
public
void run()
{
//details
}
}
13. Passing Information to Thread.
14. Returning Information from Thread.
15. Avoid Race Condition.
16. A thread specified as daemon will
cease execution when the thread that created it ends. Syntax:
threadName.setDaemon(true);
17. A thread that is not daemon is called a user
thread.
18. Synchronized
methods
public
synchronized void writeEntry(String message)
19. Every
object that has synchronized methods has a monitor. The monitor lets
only one thread at a time execute a synchronized method on the object.
20. Only
one synchronized instance method for an object can execute at any given time.
Only one synchronized static method for a class can execute at one time.
21. Synchronized
Blocks
A code block can be declared as a
synchronized on an object. Only one synchronized code block for an object can
execute at any given time.
synchronized
(System.out)
{
System.out.print(input
+ ": ");
for
(int i = 0; i < digest.length; i++)
{
System.out.print(digest[i]
+ " ");
}
System.out.println(
);
}
22. Thread Scheduling
- All important threads get
at least some time to run and that the more important threads get more
time.
- Threads should execute in
a reasonable order.(e.g. not in series)
- Thread should not suffer
through starvation.
23. Thread Priority
- Priority is specified as
an integer ranging from 1(lowest) to 10(highest).
- When multiple threads are
able to run, generally the VM will run only the highest-priority thread,
- public final void setPriority(int
newPriority)
- public final int getPriority( )
24. Suspension of Thread
There are 10 ways a thread can pause in favor of other
threads or indicate that it is ready to pause. These are:
- It can block on I/O.
- It can block on a
synchronized object.
A thread can wait on an object it has locked. While
waiting, it releases the lock on the object and pauses until it is notified by
some other thread. Another thread changes the object in some way, notifies the
thread waiting on that object, and then continues.
- It can yield.
- It can go to sleep.
A thread that goes to sleep does hold onto all the locks
it's grabbed. Consequently, other threads that need the same locks will be
blocked even if the CPU is available.
- It can join another
thread.
- It can wait on an object.
- It can finish.
There's a nontrivial amount of overhead for the virtual
machine in setting up and tearing down threads.
- It can be preempted by a
higher-priority thread.
- It can be suspended.
No comments:
Post a Comment