6.11 Write the Printtask class implements the Runnable interface, outputs integers from 0 to n-1, and N initializes the Printtask object when it is created
2.2 Using a lambda expression to overwrite 6-3
Thread t1 = new Thread(() -> {System.out.println(mainThreadName);System.out.println(Thread.currentThread().getName());System.out.println(Arrays.toString(Thread.class.getInterfaces()));});
2.3 Title: 6-2 (runnable and stop thread). Answer: How do I need to properly stop a running thread?
By defining public void Stopme (); Used to terminate a thread, stop the thread by changing the value of the flag to exit the loop
3. Mutex Access 3.1 Modify the Testunsynchronizedthread.java source code so that it can be accessed synchronously. (Key code, need to appear number)
Reference: Java multi-thread executor, Executorservice, executors, callable, future and Futuretask
4. Mutually exclusive access and synchronous access
Complete episode 6-4 (mutually exclusive access) with 6-5 (synchronous access)
4.1 In addition to using the synchronized modification method to achieve mutually exclusive synchronous access, what else can use synchronized to achieve mutually exclusive synchronous access, using code description (please show the relevant code and number)?
Synchronizing code blocks
//201621123083 孙江涛public void withdraw(int money) { synchronized(this) { balance-=money; } }
4.2 What is the difference between a synchronous code block and a synchronization method?
The synchronous code block is locked by using the Synchonrized keyword inside the method, and the synchronization method is locked directly before the method name plus the Synchonrized keyword.
4.3 What is the principle of implementing mutually exclusive access? Use the object lock concept and describe it in conjunction with the corresponding code block. How does the state of the thread change when the program executes the synchronized synchronous code block or synchronous method?
The program allows only one thread to access a shared resource within a specified time by giving a sync lock on the shared resource
When the program executes a synchronized synchronous code block or synchronous method, the thread generates a synchronous lock into the lock Pool
4.4 What keywords are used in Java Multi-threading to implement communication between threads, so that threads work together?
Synchronized, Wait (), notify ()
5. Collaboration between Threads: Producer consumer issues 5.1 run Myproducerconsumertest.java. The result of the normal operation should be 0 goods left in the warehouse. Run multiple times, observe the results, and answer: Is the result normal? What's not normal? Why?
Received abnormal, the goods in the warehouse is not 0, the speed of the library and into the library is different, the two did not exchange information, resulting in incorrect results
5.2 Use synchronized, wait, notify solve the problem (key code, need to appear number)
6. Object-oriented design job-Library Management System 6.1 system of the functional modules table, the table reflects the owner of each module. 6.2 Run the video 6.3 Explain the module you are responsible for, and paste the key code of your own module (the number and name appears).