遇到一个笔试题:
请写出线程同步的方法!
网上搜索了半天没一个看懂得
我对线程的认识少之又少。谁能给一个简单点得线程同步代码 最好有些注释
谢谢
package synh;
/*class ThreadDemo implements Runnable{
int tickets = 100;//票数
String str = new String("");
public void run(){
while(true){
/*
分析synchronized方法
*synchronized代码块中的语句只能有一个线程在执行
*
*任意一个对象都有一个标志位,有1和0两种状态
*当程序执行到synchronized代码块的时候线程会检查对象的标志位是1还是0
*如果是1则执行程序,同是将对象的标志位设置为0,其他线程执行到synchronized代码块时一看对象标志位为0
*则线程会阻塞,一直等到对象的标志位为1再执行下面的程序
*本程序中一共产生了4个线程,当一个线程执行synchronized代码块的时候其他三个线程会发生阻塞
*第一个线程执行完synchronized代码块后会把对象的标志位设置为1,其他的三个线程中的其中一个
*会抢到synchronized代码块的执行权,同时会把对象的标志位设置为0,就以这样的顺序循环执行
*
*我们也可以把对象标志位看成一个监视器,当一个线程执行到synchronized代码块的时候会检查监视器的
*状态,一个刚执行完synchronized代码块的线程也可以再次检查监视器并执行synchronized代码块
*
*多个线程要实现同步,必须使用相同的监视器对象(本例中4个线程都是用的str同一个对象),
*如果多个线程使用的不是同一个监视器对象则达不到同步的效果
*
synchronized(str){
if(tickets > 0){
try{Thread.sleep(1);}catch(Exception e){}
System.out.println(Thread.currentThread().getName() + " is salling ticket --- " + tickets--);
}
}
}
}
}/
class ThreadDemo implements Runnable{
int tickets = 100;//票数
String str = new String("");
//在一个方法里实现同步
public void run(){
while(true){
sale();
}
}
/*
*同步方法和同步块道理一样,这里就不再说了
*当一个线程执行同步方法的时候其他的方法也不能再执行同步方法中的语句
*/
public synchronized void sale(){
if(tickets > 0){
try{Thread.sleep(1);}catch(Exception e){}//模拟不同步的实现
System.out.println(Thread.currentThread().getName() + " is salling ticket --- " + tickets--);
}
}
}
class ThreadTest{
public static void main(String [] args){
ThreadDemo td = new ThreadDemo();
/*
*生成4个线程
*
*/
new Thread(td).start();
new Thread(td).start();
new Thread(td).start();
new Thread(td).start();
}
}
看完了线程间的同步,再看看同步方法和同步代码块之间的同步
package synch;
class ThreadDemo implements Runnable{
int tickets = 100;//票数
String str = new String("");
//在一个方法里实现同步
public void run(){
if(str.equals("method")){
while(true){
sale();
}
}
else{
while(true){
synchronized(this){
if(tickets > 0){
try{Thread.sleep(1);}catch(Exception e){}
System.out.println(Thread.currentThread().getName() + " is salling ticket --- " + tickets--);
}
}
}
}
}
/*
*同步方法和同步块道理一样,这里就不再说了
*当一个线程执行同步方法的时候其他的方法也不能再执行同步方法中的语句
*/
public synchronized void sale(){
if(tickets > 0){
try{Thread.sleep(1);}catch(Exception e){}//模拟不同步的实现
System.out.println("method: " + Thread.currentThread().getName() + " is salling ticket --- " + tickets--);
}
}
}
class ThreadTest1{
public static void main(String [] args){
ThreadDemo td = new ThreadDemo();
/*
*生成4个线程
*
*/
new Thread(td).start();
//这里写这个sleep()是为了让CPU转到当前线程来执行
try{Thread.sleep(1);}catch(Exception e){} td.str = "method";
new Thread(td).start();
//new Thread(td).start();
//new Thread(td).start();
}
}
这里最关键的一点就是要想让同步方法和同步代码块同步,两者就要使用相同的监视器对象
本程序中用的同一个监视器对象是this
我帮你找一些吧,你认真看看就明白了
[url]http://tech.it168.com/j/2008-01-30/200801302324557.shtml[/url]
[url]http://blog.csdn.net/hw_cncn/archive/2009/02/24/3931286.aspx[/url]
[url]http://www.javaresearch.org/article/10609.htm[/url]
[url]http://www.javaresearch.org/article/10646.htm[/url]
[url]
http://lyovercome.iteye.com/blog/145584
[/url]
package sang1;
public class TextDeadLock implements Runnable{
public int flag = 1;
static Object o1 = new Object(),o2 = new Object();
public void run(){
System.out.println("flag="+flag);
if(flag==1){
synchronized(o1){
try{
Thread.sleep(100);
}catch(Exception e){
e.printStackTrace();
}
synchronized(o2){
System.out.println("1");
}
}
}
if(flag==0){
synchronized(o2){
try{
Thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}
synchronized(o1){
System.out.println("0");
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TextDeadLock td1 = new TextDeadLock();
TextDeadLock td2 = new TextDeadLock();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.start();
t2.start();
}
}