java多线程同步块(synchronized(Object o){})的一个例程

java多线程同步块的一个例程,觉得对于理解同步快很有好处,分享之

源代码:

class Produer implements Runnable
{
	Q q;
	public Produer(Q q){
		this.q=q;
	}
	public void run(){
		int i=0;
		while(true){
			synchronized(q){
				if( i==0){	
					q.name="zhangsan";
					q.sex="male2222";
				}else{
					q.name="lisi";
					q.sex="famale11111111111";
				}
			}
                        try{
				Thread.sleep(1);
			}catch(Exception e){

			}       //在同步快结束时应该有个延时,将同步对象让给另一个线程,否则另一个线程的同步可能会长时间阻塞。
			i=(i+1)%2;
		}
	}
}




class Consumer implements Runnable{
	Q q;
	public Consumer(Q q){
		this.q=q;
	}
	public void run(){
		while(true){
			synchronized(q){
				System.out.print(q.name);
				System.out.println(q.sex);
			}
                        try{
				Thread.sleep(1);
			}catch(Exception e){

			}
		}
	}
}



class Q{
	String name="unknown";
	String sex="unknow";
}

class ThreadCommunation{
	public static void main(String[] args){
		Q q=new Q();
		new Thread(new Produer(q)).start();
		new Thread(new Consumer(q)).start();
	}
}

运行效果:

java多线程同步块(synchronized(Object o){})的一个例程_第1张图片


---------------------------------------------------------------------------

如下的程序,将更加清晰地看出同步块的同步效果

源代码:

public class TestM {
   public static void main(String[] args){
	   Object o=new Object();
	   Thread th1=new Thread(new Thread1(o));
	   Thread th2=new Thread(new Thread2(o));
	   th1.start();
	   th2.start();
   }
}


class Thread1 implements Runnable{
	Object o;
	public Thread1(Object o){
		this.o=o;
	}

	public void run() {
		while(true){
			synchronized(o){
				System.out.println("Thread1---->1111");
				try {
					Thread.sleep(100);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("Thread1---->2222");
				try {
					Thread.sleep(100);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("Thread1---->3333");
				try {
					Thread.sleep(100);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("Thread1---->4444");
			}
			try {
				Thread.sleep(100);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
}

class Thread2 implements Runnable{
	Object o;
	public Thread2(Object o){
		this.o=o;
	}
	
	public void run(){
		while(true){
			synchronized(o){
				System.out.println("Thread2---->aaaaaaaaaaaaa");
				try {
					Thread.sleep(100);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("Thread2---->bbbbbbbbbbbb");
				try {
					Thread.sleep(100);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("Thread2---->cccccccccccc");
				try {
					Thread.sleep(100);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("Thread2---->ddddddddddddd");
			}
			try {
				Thread.sleep(100);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

运行效果:

java多线程同步块(synchronized(Object o){})的一个例程_第2张图片

可见,利用同步块可以保证一个线程在一个代码块中访问同步资源未结束时,阻塞其他线程访问这个同步资源,保证了操作的一致性。

你可能感兴趣的:(java,synchronized,同步块)