不知道为什么线程不能同步,waite到底是让线程停到那个地方,比如这个里面waite过后是不是要接着执行其下面的语句,谢谢!
public class ThreadTest extends Thread
{
static int x=0;
static boolean readed=true;
ThreadTest(String s)
{
super(s);
}
public synchronized void chgdat()
{
while(!readed)
{
try
{
wait();
}
catch (InterruptedException e)
{
}
}
x++;
readed=false;
notifyAll();
}
public synchronized void showdat()
{
while(readed)
{
try
{
wait();
}
catch(InterruptedException e)
{
}
}
System.out.println("x= "+x);
readed=true;
notifyAll();
}
public void run()
{
while(x<1000)
{
if(Thread.currentThread().getName().equals("T1"))
{
chgdat();
System.out.println("chgdat is do!");
}
else
{
showdat();
System.out.println("showdat is do!");
}
}
}
public static void main(String[] args)
{
ThreadTest x,y;
x=new ThreadTest("T1");
y=new ThreadTest("T2");
x.start();
y.start();
}}
public class ThreadTest extends Thread
{
static int x=0;
static boolean readed=true;
ThreadTest(String s)
{
super(s);
}
public synchronized void chgdat()
{
while(!readed)
{
try
{
wait();
}
catch (InterruptedException e)
{
}
}
x++;
readed=false;
notifyAll();
}
public synchronized void showdat()
{
while(readed)
{
try
{
wait();
}
catch(InterruptedException e)
{
}
}
System.out.println("x= "+x);
readed=true;
notifyAll();
}
public void run()
{
while(x<1000)
{
if(Thread.currentThread().getName().equals("T1"))
{
chgdat();
System.out.println("chgdat is do!");
}
else
{
showdat();
System.out.println("showdat is do!");
}
}
}
public static void main(String[] args)
{
ThreadTest x,y;
x=new ThreadTest("T1");
y=new ThreadTest("T2");
x.start();
y.start();
}}
x=new ThreadTest("T1");
y=new ThreadTest("T2");
x.start();
y.start();
这样创建多线程 synchronized 不起作用
如上所说.x= new ThreadTest("T1");
x.start();
x.start();
你可以T1与T2互斥地使用readed,只有T2去把数据读了,T1才可以修改
其实就是个buffer为1生产者消费者问题,只有buffer中有了新的数据(readed == false)T2才读,只有buffer中的数据被T2读出去了(readed == true)T1才进行修改,如果不是上面的情况就想将自己阻塞(调用wait()),当一个线程完成自己的任务(读数据或者是修改数据)后,它就有责任将等待在阻塞队列上的线程唤醒(Java中推荐用
notifyAll(),除非你确实知道每个线程是因为什么而阻塞)那怎样才能实现互斥呢?没一个Object都持有一把锁,只要它的锁被一个线程占有了,其他线程就不能再占有,
LZ的程序中T1与T2根本就没有进行同步,synchronized方法有跟没有一样,但是又都共享了class variable readed,它是不能保证正确同步与互斥的,因为假如推进的顺序不当就会出现死锁(一种线程都不能向前推进的僵局),只有获得锁才能保证,所以一种方法是让两个线程都共享一个Runnable对象(当然应该还可以让它们共享别的对象,LZ可以自己去写写)。还有一点就你那打印"chgdat is do!"与另一句都不能放那里,那个地方是不能保证同步的,给修改了一下,你自己想想吧。public class ThreadTest extends Thread { public static void main(String[] args) {
MyRunnable r = new MyRunnable();
new Thread(r, "T1").start();
new Thread(r, "T2").start();
}
}class MyRunnable implements Runnable {
static int x=0;
static boolean readed=true; public synchronized void chgdat() {
while(!readed) {
try {
wait();
} catch (InterruptedException e) {
}
}
x++;
System.out.println("chgdat Done!");
readed=false;
notifyAll();
} public synchronized void showdat() {
while(readed) {
try {
wait();
} catch(InterruptedException e) {
}
}
System.out.println("x= "+x);
System.out.println("showdat Done!");
readed=true;
notifyAll();
} public void run() {
while(x<10) {
if(Thread.currentThread().getName().equals("T1")) {
chgdat();
} else {
showdat();
}
}
}
}