Java multi-threading (D) synchronized synchronization method and synchronized block

Java multi-threaded in order to solve security problems caused by using a synchronous manner to address security issues.

Address security issues multithreading, Java provides three ways:

Synchronization method

Sync block

java juc package (locking mechanism)

Thread unsafe generate multiple threads simultaneously using the same resource, the thread will come with insecurity.

E.g:

public class Cinema {

    public static void main(String[] args) {
        Office TicketOffice = new new TicketOffice ();
         // create three conductor 
        the Thread seller1 = new new the Thread (Office);
        Thread seller2 = new Thread(office);
        Thread seller3 = new Thread(office);
        seller1.start();
        seller2.start();
        seller3.start();
     }
}

class TicketOffice the implements Runnable {
     // define the 10 movie ticket 
    int Ticket = 10 ;

    @Override
    public void run() {
        while (ticket > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace ();
            }
            // start selling movie tickets, each ticket sold a movie, automatically minus 1 
            System.out.println (Thread.currentThread (). GetName () + "is selling the first" + (ticket--) + "Zhang ticket "! );
        }
    }
}

result:

There have been repeated ticketing (selling more than one vote, to marry more than one woman) and the situation is negative votes.

Plus Thread.sleep () just to get an increase in the probability of error

Thread-0 is selling tickets 10! 
The Thread -1 10 is selling tickets! 
The Thread -2 10 is selling tickets! 
The Thread -1 9 is selling tickets! 
The Thread -2 9 is selling tickets! 
the Thread -0 9 is selling tickets! 
the Thread -1 8 is selling tickets! 
the Thread -2 7 is selling tickets! 
the Thread -0 8 is selling tickets! 
the Thread -0 6 is selling tickets! 
the Thread -1 4 is selling tickets! 
the Thread -2 5 is selling tickets! 
the Thread -0 3 is selling tickets! 
the Thread -2 2 is selling tickets! 
the Thread -1 3 is selling tickets! 
the Thread -1 first ticket is sold! 
the Thread -2 -1 is selling tickets! 
the Thread -0 0 is selling tickets!

 

Sync block

The following is the code syntax sync block

the synchronized (obj) {
     // local needs synchronization
     // obj may be any shared resource (object), the synchronization code is the synchronization target blocks 
}

 

To solve the problem by following the synchronization code blocks Ticketing

public class Cinema {


    public static void main(String[] args) {
        Office TicketOffice = new new TicketOffice ();
         // create three conductor 
        the Thread seller1 = new new the Thread (Office);
        Thread seller2 = new Thread(office);
        Thread seller3 = new Thread(office);
        seller1.start();
        seller2.start();
        seller3.start();
    }
}

class TicketOffice the implements Runnable {
     // define the 10 movie ticket 
    int Ticket = 10 ;
    Obj Object = new new Object (); // this must be written in class, do not write the method in

    // write in the class which is the sharing of resources, written on the inside approach, each thread has its own resources, it is not shared by the 
    @Override
     public  void RUN () {
         the synchronized (obj) {
             the while (Ticket> 0 ) {
                 the try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
                // start selling movie tickets did not sell a movie ticket, automatically minus 1 
                System.out.println (Thread.currentThread (). GetName () + "is selling the first" + (ticket--) + "Zhang ticket "! );
            }
        }
    }
}

 

Synchronization method

Modifier synchronized method return value name () {
     // Method member 
}

 

To solve the problem by synchronizing thread-safe method

public class Cinema {


    public static void main(String[] args) {
        Office TicketOffice = new new TicketOffice ();
         // create three conductor 
        the Thread seller1 = new new the Thread (Office);
        Thread seller2 = new Thread(office);
        Thread seller3 = new Thread(office);
        seller1.start();
        seller2.start();
        seller3.start();
    }
}

class TicketOffice the implements Runnable {
     // define the 10 movie ticket 
    int Ticket = 10 ;
    Obj Object = new new Object (); // this must be written off in class, do not write the method Lee

    // write in the class which is the sharing of resources, written on the inside approach, each thread has its own resources, it is not shared by the 
    @Override
     public  void RUN () {
        sell ticket ();
    }

    // synchronization method 
    public  the synchronized  void sellTicket () {
         the while (Ticket> 0 ) {
             the try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace ();
            }
            // start selling movie tickets did not sell a movie ticket, automatically minus 1 
            System.out.println (Thread.currentThread (). GetName () + "is selling the first" + (ticket--) + "Zhang ticket "! );
        }
    }
}

 

 

 

References:

https://www.geeksforgeeks.org/synchronized-in-java/

https://www.cnblogs.com/xckxue/p/8685675.html

Ali programmer:

https://www.jianshu.com/p/f9b1159d4fde

https://www.jianshu.com/p/2ed498b43628

https://segmentfault.com/a/1190000013512810

Guess you like

Origin www.cnblogs.com/majestyking/p/12437426.html