public class TestThread
{
public static void main(String [] args)
{
Test t=new Test();
new Thread(t).start();
try
{
Thread.sleep(1);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
t.str=new String("method");
new Thread(t).start();
}
}
class Test implements Runnable
{
int tickets=100;
String str=new String("");
public void run()
{
if(str.equals("method"))
{
while(true)
{
sale();
}
}
else
{
synchronized(str)
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
synchronized(this){}//等待this监视器
System.out.println("同步代码块:");
System.out.println(Thread.currentThread().getName()+"is saling ticket"+tickets--);
}
}
}
}
public synchronized void sale()
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
synchronized(str){}//等待str监视器
System.out.println("同步函数:");
System.out.println(Thread.currentThread().getName()+"is saling ticket"+tickets--);
}
}
}
{
public static void main(String [] args)
{
Test t=new Test();
new Thread(t).start();
try
{
Thread.sleep(1);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
t.str=new String("method");
new Thread(t).start();
}
}
class Test implements Runnable
{
int tickets=100;
String str=new String("");
public void run()
{
if(str.equals("method"))
{
while(true)
{
sale();
}
}
else
{
synchronized(str)
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
synchronized(this){}//等待this监视器
System.out.println("同步代码块:");
System.out.println(Thread.currentThread().getName()+"is saling ticket"+tickets--);
}
}
}
}
public synchronized void sale()
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
synchronized(str){}//等待str监视器
System.out.println("同步函数:");
System.out.println(Thread.currentThread().getName()+"is saling ticket"+tickets--);
}
}
}
synchronized(this){}//等待this监视器
什么叫做:等待this监视器?等待是什么含义?
{
public static void main(String [] args)
{
Test t=new Test();
new Thread(t).start();
try
{
Thread.sleep(1);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
t.str=new String("method");
new Thread(t).start();
}
}
class Test implements Runnable
{
int tickets=100;
String str=new String("");
public void run()
{
if(str.equals("method")) //当start()方法启动新创建的线程时因为t.str=new String("method")进行赋值使if成立
{
while(true)//while(tickets>0) 这里你一直让while条件成立导致循环结束后不能退出,进入死循环
{
sale();
}
}
else
{
synchronized(str)
{ //一个简单的同步语句块,在这个语句块中一次只能调用一个线程
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
synchronized(this){}//等待this监视器
System.out.println("同步代码块:");
System.out.println(Thread.currentThread().getName()+" is saling ticket"+tickets--); }
}
}
}
public synchronized void sale()
{ //同步中的非静态成员方法,在这个语句块中一次只能调用一个线程
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
synchronized(str){}//等待str监视器
System.out.println("同步函数:");
System.out.println(Thread.currentThread().getName()+" is saling ticket"+tickets--); }
}
}我不明白LZ为什么不产生死锁的意思 这个例子不是讲死锁问题。我简单的解释了下,我也是新手。错的地方希望大家拍砖~~~
{
public static void main(String [] args)
{
Test t=new Test();
new Thread(t).start();
try
{
Thread.sleep(1);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
t.str=new String("method");
new Thread(t).start();
}
}
class Test implements Runnable
{
int tickets=100;
String str=new String("");
public void run()
{
if(str.equals("method")) //当start()方法启动新创建的线程时因为t.str=new String("method")进行赋值使if成立
{
while(true)//while(tickets>0) 这里你一直让while条件成立导致循环结束后不能退出,进入死循环
{
sale();
}
}
else
{
synchronized(str)
{ //一个简单的同步语句块,在这个语句块中一次只能调用一个线程
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
synchronized(this){}//等待this监视器
System.out.println("同步代码块:");
System.out.println(Thread.currentThread().getName()+" is saling ticket"+tickets--); }
}
}
}
public synchronized void sale()
{ //同步中的非静态成员方法,在这个语句块中一次只能调用一个线程
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
synchronized(str){}//等待str监视器
System.out.println("同步函数:");
System.out.println(Thread.currentThread().getName()+" is saling ticket"+tickets--); }
}
}
这是一个死锁的例子,大家看一下,不对拍砖~
public class J_Lock extends Thread
{
public static Object m_objectA=new Object();
public static Object m_objectB=new Object();
J_Lock(String s)
{
super(s);
}
public static void mb_sleep()
{
try
{
Thread.sleep((long)(Math.random()*1000));
}
catch(InterruptedException e)
{
System.err.println("异常InterruptedException:"+ e);
e.printStackTrace();
}
}
public void run()
{
boolean t=true;
System.out.println(getName() +"开始运行");
for(;true;t=!t)
{
synchronized(t ? m_objectA : m_objectB)
{
System.out.println(getName()+":"+(t ? "对象A" : "对象B")+"被锁住");
mb_sleep();
synchronized(t ? m_objectB : m_objectA)
{
System.out.println(getName()+":"+(t ? "对象B" : "对象A")+"被锁住");
mb_sleep();
System.out.println(getName()+":"+(t ? "对象B" : "对象A")+"的锁打开");
}//内层同步语句块
System.out.println(getName()+":"+(t ? "对象A" : "对象B")+"的锁打开");
}//外层同步语句块
}//当A被锁住等待B来打开时,B被锁住等待A来打开。这是产生了死锁。
}
public static void main(String args[])
{
J_Lock t1 = new J_Lock("线程1");
J_Lock t2 = new J_Lock("线程2");
t1.start();
t1.start();
}
}