package com.tianmushanlu.thread; /** * Creation steps: * 1. Customize a class that inherits the Thread class. * 2. Rewrite the run method of the Thread class, and write the task code of the custom thread on the run method. * 3. Create a subclass object of Thread, and call the start method to start a thread. * * The root cause of the thread safety problem: * 1. There are two or more thread objects, and a resource is shared between threads. * 2. There are multiple statements that operate on shared resources. (Use synchronized code blocks to synchronize multiple statement operations) * * Notes for synchronized code blocks: * 1. Any object can be used as a lock object * 2. Calling the sleep method in a synchronized code block does not release the lock object. * 3. Only use synchronized code blocks when there is a real thread safety problem, otherwise it will reduce efficiency. * 4. The lock object of multi-threaded operation must be uniquely shared. Otherwise invalid. * * * */ class TicketWindow extends Thread { /** * number of votes * Non-static member variables, non-static member variable data will maintain a piece of data in each object. * *num is non-static, * Non-static member variable data will maintain one copy of data in each object, and three thread objects will have three copies. (See Figure 1) */ static Integer num = 50; public TicketWindow(String name) { super(name); } @Override public void run() { while(true) { /** * Use synchronized code blocks to synchronize multiple statement operations * In principle, any object can be used as a lock object, and all objects maintain a state inside * The synchronization mechanism in java uses the state of the object as the identifier of the lock * state = 0 -----> ON * state = 1 -----> off * Thus the lock object must be a shared object * This object must be modified with static, such as: * static Object o = new Object(); * synchronized (o){ * Code that needs to be synchronized ................. * } * *The following method is the simplest lock object, * Because the string enclosed in double quotes will not be created once it exists in the string constant word, * Thus all threads will use the lock object in the string constant word in common * synchronized ("lock object") { * Code that needs to be synchronized ................. * } * */ synchronized ("lock object") { if(num > 0) { System.out.println(Thread.currentThread().getName()+"sold "+num+" ticket"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace (); } on one--; }else{ System.out.println("Tickets are sold out........"); break; } } } } } public class ThreadDemo{ public static void main(String[] args) { /** * Create 3 windows for ticket sales */ TicketWindow thread1 = new TicketWindow("Ticket window 1"); TicketWindow thread2 = new TicketWindow("Ticket window 2"); TicketWindow thread3 = new TicketWindow("Ticket window 3"); /** * Open thread ticketing */ thread1.start(); thread2.start(); thread3.start(); } }
java thread extends Thread implementation (synchronized code block)
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=327071340&siteId=291194637
Recommended
Ranking