两个线程A,B。调用同一加锁代码块C,假如A先调用C,在A线程调用C完成之前,B线程要调用此代码块必须先等待,等A调用完成,B立马执行C.
package test; import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import net.sf.json.JSONObject; import com.hlzt.wx.util.http.WxHttpClientUtils; import com.hlzt.wx.util.msg_handler.MsgDispatcher; public class ss { public static void main(String[] args) throws UnsupportedEncodingException { ddd d=new ddd("aaaaaaaaaaaaaa"); d.start(); ddd d2=new ddd("bbbbbbbbbbbbbb"); d2.start(); } public synchronized static void aa(String str) { System.out.println("1#########"+str); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("2#########"+str); } } class ddd extends Thread{ private String aaa; public ddd(String aaa) { this.aaa = aaa; } @Override public void run() { ss.aa(aaa); } }
时间: 2024-10-01 07:49:50