Escolar Documentos
Profissional Documentos
Cultura Documentos
1 What is a Thread?
2 Why Multiple Threads
3 Java’s Built-in Thread Support.
4 Thread Priority.
5 Thread Scheduler.
6 Rules that will determine when the Thread Scheduler will switch
Threads.
7 start () and run ().
8 setPriority () and getPriority ()
9 setName() and getName ()
10 currentThread () -getting a handle to the main thread
11 Thread Constructors
12 Thread class and Runnable Interface
13 isAlive ()
14 Two ways of creating Threads
15 Using constructors instead of writing in the main method.
16 sleep() – With both the ways
17 using yield ()
18 creating multiple Threads
19 join () – using it both the ways.
20 suspend (), resume (), stop () – reason why it is not being used.
21 Synchronization
22 The monitor model for synchronization
23 Releasing the lock flag
24 Daemon & user Threads
25 ThreadGroup
26 Creating threads in a ThreadGroup
27 Methods that operate on a ThreadGroup –
28 Access Restriction Methods - setMaxPriority ()
Synchronization
- When two or more threads need to access 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 is
achieved is called synchronization.
- In java, synchronization is easy because each object has its own explicit monitor that is
automatically entered when one of the object’s synchronized methods is called. Once a
thread is inside a synchronized method, no other thread can call any other synchronized
method on the same object. To enter an object’s monitor, just call a method that has been
modified with the synchronized keyword. The general form of synchronized method is:
synchronized method()
{
statements inside the method;
}
- When a thread enters the synchronized method, it is said acquire the lock flag (key) to the
object and all the other threads will have to wait till the time, it releases the lock flag.
Therefore it is important for the thread to hand over the lock flag when it is no longer required.
- The lock flag is given back to its object automatically. When the thread that holds the lock
passes the end of the synchronized code () for which the lock was obtained, the lock is
released. Java technology ensures that the lock is always returned automatically, even if an
encountered exception or break statement transfers code execution out of a synchronized
block
class Synch1
{
public static void main(String args[])
{
Callme target = new Callme();
class ConcurrencyTest2 {
public static void main (String Args[]) {
new Thread(new WellBehavedPrintThread("A")).start();
new Thread(new WellBehavedPrintThread("B")).start();
new Thread(new WellBehavedPrintThread("C")).start();
new Thread(new WellBehavedPrintThread("D")).start();
}
}
- In the earlier example, there was only class and we were creating multiple threads from that
class and saw how synchronization could be done. Now let’s take another example, wherein,
there is a XYS class in which there are two methods store () and load () and it has a private
variable which will be inserted by the store method and taken away by the load method.
There are now two different classes class Producer that will call the store () and class
Consumer that will call the load () method.
Remember that both the classes will access the same object of the XYZ class. Now the
producer will keep on storing numbers and consumer will keep on taking numbers. Also
both the threads will be sleeping after they have used the methods. What we want is that
Producer should store only one value and the Consumer will have to take that value only
once and all the values stored by the Producer should be taken also.
class MyData {
private int Data;
- We can achieve the above kind of our result with the following code
where both the methods are synchronized.
class MyData {
private int Data;
private boolean Ready;
private boolean Taken;
public MyData() {
Ready=false;
Taken=true;
}
Consumer(Stock c)
{
this.c = c;
//t.new Thread(this,"Consumer Thread");
start();
//t.start();
}
c.getStock((int)(Math.random()*100));
}
}
}
Stock s;
Thread t;
Producer (Stock s)
{
this.s = s;
//t.new Thread(this,"Producer Thread");
start();
//t.start();
}
s.addStock((int)(Math.random()*100));
}
}
}
class Stock
{
int goods=0;
notify();
}
try
{
Thread.sleep(10000);
//p.t.join();
//c.t.join();
System.out.println("Thread Stopped");
}
catch(InterruptedException e)
{}
System.exit(0);
}
}
class SyncExample
{
try
{
Thread.sleep(1000);
System.out.println("Thread Stopped");
}
catch(InterruptedException e)
{}
System.exit(0);
}
}
Producer1(SyncStack s)
{
thestack = s;
num = counter++;
}
SyncStack thestack;
int num;
static int counter=1;
Consumer1(SyncStack s)
{
thestack = s;
num = counter++;
}
try
{
Thread.sleep((int)(Math.random() * 300));
}
catch(InterruptedException e)
{}
}
}
}
class SyncStack
{
Vector v = new Vector(400,200);
Producer(Queue queue) {
this.queue = queue;
}
class Queue {
private final static int SIZE = 10;
int array[] = new int[SIZE];
int r = 0;
int w = 0;
int count = 0;
// Increment count
++count;
// Decrement count
--count;