Java multithreading is really hard to understand.

  java, question

The source code is as follows:
package com.study.test;

/* Implementing Runnable Objects for Multithreading
* @author who
* @since 2015-05-05
* */

/* Realize Ticket Selling Function with Multithreading
* a total of 1000 tickets, each selling one ticket -1
* */
class Ticket implements Runnable {
private int tick = 1000;
boolean bb = true;
public void run() {
// TODO Auto-generated method stub
if (bb) {
while (true) {
if (tick > 0) {
synchronized (this) {
try {
System.out.println (“the current boolean value comes from the run method:” plus bb “);
Thread.sleep(20);
System.out.println(Thread.currentThread().getName()
Add “… sale” and tick–);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
bracket
bracket
bracket
bracket
} else {
while (true)
SaleTicket();
bracket
bracket

public synchronized void SaleTicket() {
 if (tick > 0) {
 try {
 System.out.println ("the current boolean value comes from the SaleTicket method:" plus bb ");
 Thread.sleep(20);
 System.out.println(Thread.currentThread().getName()
 Add "... SaleTicket" and tick--);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 bracket
 bracket
 bracket

bracket

class TicketDemo {

public static void main(String[] args) {
 // TODO Auto-generated method stub
 Ticket t = new Ticket();
 Thread t1 = new Thread(t, "thread 1:");
 Thread t2 = new Thread(t, "thread 2:");
 Thread t3 = new Thread(t, "thread 3:");
 Thread t4 = new Thread(t, "thread 4:");
 t1.start();
 Sleep20();
 t.bb = false;
 t2.start();
 Sleep20();
 t.bb = true;
 t3.start();
 Sleep20();
 t.bb = false;
 t4.start();
 bracket
 
 public static void Sleep20() {
 try {
 Thread.sleep(20);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 bracket
 bracket

bracket

The partially executed output is as follows:
The current boolean value comes from the run method: true
Thread 1: … Sale1000
The current boolean value comes from the run method: false
Thread 1: … Sale999
The current boolean value comes from the SaleTicket method: true
Thread 2: … salesticket 998
……………………………………
The current boolean value comes from the run method: false
Thread 1: … sale2
The current boolean value comes from the run method: false
Thread 1: … sale1
The current boolean value comes from the run method: false
Thread 3: … sale0
Questions are as follows:
1. According to the code, the synchronization code block of the run method will only be entered when the bb variable is true, otherwise it will be entered into the SaleTicket synchronization method, but the current boolean value of the above output is completely random.
2. The two synchronization code blocks have been processed synchronously and safely. However, there is still a “0” value, which will not appear when two threads are opened.

In run (), bb value is judged before entering the synchronization code segment, which may lead to this situation: bb value is true, and before entering the synchronization code segment, another thread modifies bb value to false. Similarly, synchronized did not protect tick as a critical zone.