2013年11月18日星期一

Multi-threaded, worship god , lock


public class Demo{
public static int rocket = 20;
public static void main(String[] args){
MyThread mt1 = new MyThread();
Thread t1 = new Thread(mt1);
MyThread2 t2 = new MyThread2();
t1.start();
t2.start();
}
}
class MyThread implements Runnable{
public  void run(){
synchronized("11"){
while(Demo.rocket>0){
 
if(Demo.rocket==1){
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
Demo.rocket--;
System.out.println("1111卖票,剩余:"+Demo.rocket+"张票");
}
}
}
}
class MyThread2 extends Thread{
public   void run(){
synchronized("11111"){
while(Demo.rocket>0){ 
 
Demo.rocket--;
System.out.println("22222222卖票,剩余 :"+Demo.rocket+"张票........");
}
}
}
}
 



Results:
1111 ticket , remaining : 19 helpful
22222222 sell tickets remaining : 18 helpful .....
1111 ticket , remaining : 17 helpful
22222222 sell tickets remaining : 16 helpful .....
1111 ticket , remaining : 15 helpful
1111 ticket , remaining : 13 helpful
22222222 sell tickets remaining : 14 helpful .....
1111 ticket , remaining : 12 tickets
22222222 sell tickets remaining : 11 helpful .....
22222222 sell tickets remaining : 9 helpful ......
22222222 sell tickets remaining : 8 helpful ......
1111 ticket , remaining : 10 helpful
1111 ticket , remaining : 6 tickets
1111 selling tickets remaining : 5 helpful
22222222 sell tickets remaining : 7 helpful ......
22222222 sell tickets remaining : 3 helpful ......
1111 ticket , remaining : 4 helpful
1111 selling tickets remaining : 1 tickets
22222222 sell tickets remaining : 2 tickets ......
22222222 sell tickets remaining : 0 tickets ......
1111 ticket , remaining : -1 tickets
why results have -1 ah ? How to solve ?
------ Solution ---------------------------------------- ----
-1 is the thread synchronization phenomena have caused , when the judge while condition is certainly satisfied , into the implementation of the time , another thread will Demo.rocket execution result is 0 , so ......
------ Solution ------------------------------------------ -
to lock this object demo
package com.xiehou.th;

public class Demo {

public static int rocket = 20;

public static void main(String[] args) {

Demo demo=new Demo();

MyThread mt1 = new MyThread(demo);
Thread t1 = new Thread(mt1);
MyThread2 t2 = new MyThread2(demo);
t1.start();
t2.start();
}
}

class MyThread implements Runnable {

public Demo demo;

public MyThread(Demo demo){
this.demo=demo;
}

public void run() {
synchronized (demo) {
while (Demo.rocket > 0) {

if (Demo.rocket == 1) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
Demo.rocket--;
System.out.println("[1111]卖第:" + Demo.rocket + "张票");
}
}
}
}

class MyThread2 extends Thread {

   public Demo demo;

public MyThread2(Demo demo){
this.demo=demo;
}

public void run() {
synchronized (demo) {
while (Demo.rocket > 0) {

Demo.rocket--;
System.out.println("[2222]第 :" + Demo.rocket + "张票");
}
}
}
}

------ Solution ------------------------------------- -------
you two are not the same lock , a lock does not work , got to lock the same object .
------ For reference only -------------------------------------- -
you lock rocket variables , other threads do not get in the right, or you can lock the Demo class lines . You still see Thread Synchronization bar
------ For reference only ------------------------------- --------
synchronized (this), on ok !
------ For reference only -------------------------------------- -

public class Demo {
public static int rocket = 20;
public static void main (String [] args) {
MyThread mt1 = new MyThread ();
Thread t1 = new Thread (mt1);
MyThread2 t2 = new MyThread2 ();
t1.start ();
t2.start ();
}
}
class MyThread implements Runnable {
public void run () {
synchronized (this) {
while (Demo.rocket> 0) {

if (Demo.rocket == 1) {
try {
Thread.sleep (1000);
} catch (Exception e) {
e.printStackTrace ();
}
}
Demo.rocket--;
System.out.println ("1111 ticket , remaining :" + Demo.rocket + " tickets " ) ;
}
}
}
}
class MyThread2 extends Thread {
public void run () {
synchronized (this) {
while (Demo.rocket> 0) {

Demo.rocket--;
System.out.println ("22222222 selling tickets remaining :" + Demo.rocket + " tickets ........ " ) ;
}
}
}
}


------ For reference only ---------------------------------- -----
Thank above answer

没有评论:

发表评论