程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Multi-Thread 1: how to use synchronized

Multi-Thread 1: how to use synchronized

編輯:C++入門知識

1. synchronized

If two threads are using the same function( here we use output to print out string) of another instance, if we want to make sure that these two threads are not disturbing each other.
public class TraditionalThreadSynchronize {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new TraditionalThreadSynchronize().initial();
	}
	
	private void initial(){
		final Outputter outputter = new Outputter();
		new Thread(new Runnable(){  //this is the first theread
			public void run(){
				while(true){
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					outputter.output("aaaaaaaaaaa"); //the thread want to use outputer print a string.
				}
					
			}
			
		}).start();
		
		new Thread(new Runnable(){//thread two
			public void run(){
				while(true){
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					outputter.output("bbbbbbbbbbbbb");  //thread two also want to use the function to print
				}
					
			}
			
		}).start();
	}
	
	class Outputter{
		public void output(String name){
			int len = name.length();
			for(int i= 0;i
If code won't escape disturbing each other. The calling function must be the same one from the same instance!
Error Example 1:
new Thread(new Runnable(){
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					new outputer.output("bbbbbbbb");
				}
				
			}
		}).start();

if we use new outputer.output("bbbbbbbbb"), still won't work. since the two threads are calling different function from two different instance. So one thing is clear that we have to add synchronized key word and it should be added to the same Instance.
ANSWER 1: we can make it possible by letting two threads calling for the same function, of the same Instance so we can change the output function to:
public synchronized void output(String name){
			int len = name.length();
			for(int i=0;i
// Synchronized add to the function is the same as synchronized(this). the Instance is sychronized for this function

ANSWER 2: Since we already know the key point is to make the same Instance be synchronized, so we can make the two threads call for different functions( output1, and output3), as long as the instance is synchronized(which is also easy to do: both of the two functions should add synchronized key word)
public void output(String name){
	int len = name.length();
	synchronized (this) 
	{
	for(int i=0;i
ANSWER 3: in the case that the function is a static method If the method is a static method, since static method is initialized before Instance, if we still want to synchronize two method, One way is to make both of the two method static synchronized, another way, we can synchronize on the instance's class
public void output(String name){
	int len = name.length();
	synchronized (Outputer.class) 
	{
		for(int i=0;i
		System.out.println();
	}
}		

public synchronized void output2(String name){
	int len = name.length();
	for(int i=0;i




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved