/** * * @author jash */ public class ThreadTest{ private String data;//临界资源 public static void main(String[] args) { ThreadTest tt = new ThreadTest(); tt.new ProducerThread().start(); tt.new ConsumerThread().start(); }
public synchronized void produce(){ System.out.println("--Enter method produce()"); for(int i=0;i<1000;i++);//延时循环 data = "Something";//给data赋值 }
public synchronized void consume(){ System.out.println("--Enter method consume()"); int length = data.length();//如果data还没有在produce()里面赋值,这里应该会抛出NullPointerException System.out.println("Length of data is "+length); }
class ProducerThread extends Thread{ public void run() { produce(); } } class ConsumerThread extends Thread{ public void run() { consume(); } } } 这段代码可以演示消费者和生产者之间的同步关系,produce()与consume()方法都加了sychronized关键字用于线程之间的同步,如果把这个关键字从这两个方法里面都去掉,然后执行这个程序20遍左右,总有几次在consume()方法里面会抛出异常,这是因为还没有来得及在produce()方法里面对临界资源赋值,就被其他线程调用了consume()方法.加上关键字后,不管运行多少次,程序都不会出现这种异常.
class Producer extends Thread { private HoldIntegerSynchronized sharedObject; public Producer(HoldIntegerSynchronized shared) { super("Producer"); sharedObject = shared; } public void run() { for (int count = 1; count <= 10; count++) { try { Thread.sleep((int) (Math.random() * 3000)); } catch (InterruptedException e) { System.err.println(e.toString()); } sharedObject.setSharedInt(count); } System.err.println(getName() + " finished producing values" + "\nTerminating " + getName()); } }class Consumer extends Thread { private HoldIntegerSynchronized sharedObject; public Consumer(HoldIntegerSynchronized shared) { super("Producer"); sharedObject = shared; } public void run() { int value, sum = 0; do { try { Thread.sleep((int) (Math.random() * 3000)); } catch (InterruptedException e) { System.err.println(e.toString()); } value = sharedObject.getSharedInt(); sum += value; } while (value != 10); System.err.println(getName() + " retrieved values totaling: " + sum + "\nTerminating " + getName()); } }class HoldIntegerSynchronized { private int sharedInt = -1; private boolean writeable = true; public synchronized void setSharedInt(int value) { while (!writeable) { try { wait(); } catch (InterruptedException e) { System.err.println(e.toString()); } } System.err.println(Thread.currentThread().getName()+ " setting sharedInt to " + value); sharedInt = value; writeable = false; notify(); } public synchronized int getSharedInt() { while (writeable) { try { wait(); } catch (InterruptedException e) { System.err.println(e.toString()); } } writeable = true; notify(); System.err.println(Thread.currentThread().getName()+ " retriving sharedInt value " + sharedInt); return sharedInt; } } public class ShareCell{ public static void main(String args[]){ HoldIntegerSynchronized sharedobject=new HoldIntegerSynchronized(); Producer producer=new Producer(sharedobject); Consumer consumer=new Consumer(sharedobject); producer.start(); consumer.start();
*
* @author jash
*/
public class ThreadTest{
private String data;//临界资源
public static void main(String[] args) {
ThreadTest tt = new ThreadTest();
tt.new ProducerThread().start();
tt.new ConsumerThread().start();
}
public synchronized void produce(){
System.out.println("--Enter method produce()");
for(int i=0;i<1000;i++);//延时循环
data = "Something";//给data赋值
}
public synchronized void consume(){
System.out.println("--Enter method consume()");
int length = data.length();//如果data还没有在produce()里面赋值,这里应该会抛出NullPointerException
System.out.println("Length of data is "+length);
}
class ProducerThread extends Thread{
public void run() {
produce();
}
}
class ConsumerThread extends Thread{
public void run() {
consume();
}
}
}
这段代码可以演示消费者和生产者之间的同步关系,produce()与consume()方法都加了sychronized关键字用于线程之间的同步,如果把这个关键字从这两个方法里面都去掉,然后执行这个程序20遍左右,总有几次在consume()方法里面会抛出异常,这是因为还没有来得及在produce()方法里面对临界资源赋值,就被其他线程调用了consume()方法.加上关键字后,不管运行多少次,程序都不会出现这种异常.
private HoldIntegerSynchronized sharedObject; public Producer(HoldIntegerSynchronized shared) {
super("Producer");
sharedObject = shared;
} public void run() {
for (int count = 1; count <= 10; count++) {
try {
Thread.sleep((int) (Math.random() * 3000));
} catch (InterruptedException e) {
System.err.println(e.toString());
}
sharedObject.setSharedInt(count);
}
System.err.println(getName() + " finished producing values"
+ "\nTerminating " + getName());
}
}class Consumer extends Thread {
private HoldIntegerSynchronized sharedObject; public Consumer(HoldIntegerSynchronized shared) {
super("Producer");
sharedObject = shared;
} public void run() {
int value, sum = 0;
do {
try {
Thread.sleep((int) (Math.random() * 3000));
} catch (InterruptedException e) {
System.err.println(e.toString());
}
value = sharedObject.getSharedInt();
sum += value;
} while (value != 10);
System.err.println(getName() + " retrieved values totaling: " + sum
+ "\nTerminating " + getName());
}
}class HoldIntegerSynchronized {
private int sharedInt = -1; private boolean writeable = true; public synchronized void setSharedInt(int value) {
while (!writeable) {
try {
wait();
} catch (InterruptedException e) {
System.err.println(e.toString());
}
}
System.err.println(Thread.currentThread().getName()+ " setting sharedInt to " + value);
sharedInt = value;
writeable = false;
notify();
} public synchronized int getSharedInt() {
while (writeable) {
try {
wait();
} catch (InterruptedException e) {
System.err.println(e.toString());
}
}
writeable = true;
notify();
System.err.println(Thread.currentThread().getName()+ " retriving sharedInt value " + sharedInt);
return sharedInt;
}
}
public class ShareCell{
public static void main(String args[]){
HoldIntegerSynchronized sharedobject=new HoldIntegerSynchronized();
Producer producer=new Producer(sharedobject);
Consumer consumer=new Consumer(sharedobject);
producer.start();
consumer.start();
}
}
不过jdk1.5种提供了java.util.concurrent,研究它吧!
http://www.chinablog.com/user2/leave/archives/2006/129429.shtml
就明白synchronized怎么用了
BTW:这里不涉及1.5新功能