Homework 1

Posted by Java Course Staff on September 17, 2016

Homework 1

Topic: Multithread Programming & Debugging
Due: (GMT+8) 2016-10-09 09:00 SUN


Java Debug Hacks

Material

First, please read the tutorial of debug hacks. Link: Java Debug Hacks (CHN)
And clone the code to practice with: code repo

Objectives

Objective A

Practice adding watchpoint, modifying variable, conditional breakpoint, switching threads. Note that if you are debugging the multithreaded version of code, you should combine conditional breakpoint and thread switching together. This will help you get an experience of thread execution.

Objective B

Answer: in function quickMultiply, what does a CyclicBarrier serve for? Why the barrier value is 5 instead of 4? What’s the consequence of changing it into 4?

Submission

For Objective A, you are expected to make a screenshot for each debug hack, and give a short explanation of your purpose for each one.
For both objectives, you can write them into a single document or two(doc, docx, pdf, markdown). Append your document(s) to your “Doubly-linked list” homework.

Doubly Linked List

Materials

The material you need is the source code of a uni-threaded linked list. code repo.

Readings

You are expected to read about how the following things are done in Java. - create a thread - start a thread - protect critical data

At least, you have to master the usage of Mutex. If you apply an advanced synchronization mechanism, such as a reader-writer lock, to improve the degree of concurrency - you will get extra credits.

建议阅读《Java编程思想》第四版中Concurrency(并发)那一章。我们也推出了多线程编程的入门教程,传送门

Requirements

Requirement A

_ Updated at 2016-09-29 >>> _

Write a test case in Main_A.main() which starts multiple threads to read/write the same node. 不需要再给getter或setter单独上锁。 Ensure that the data field of a Node will not be corrupted if read/written by multiple threads.

写代码的时候,单独的get/set往往不是一个完整的业务,一个完整的业务是get-compute-set。上锁要上给一个完整的业务,因此给get或set单独上锁是没有意义的。

_ Updated at 2016-09-29 <<< _

Hint for Requirement A: You can create multiple threads to increase/decrease the value of data. If the final result of data is mathematically correct, then your program must be correct.

Requirement B

Refactor MyList with a locking mechanism you like. Ensure that the data structure of the doubly-linked list will not be corrupted if some nodes are inserted/removed simultaneously by multiple threads. Also, write a test case in Main_B.main() which starts multiple threads to insert into / remove from the same list.

Hint for Requirement B: Traverse and back-traverse your list to check whether or not your list is broken.

_ Updated at 2016-10-03 >>> _

如果大家想以细化锁粒度的方式来提高并发度,可以放宽锁的限制,任何代码段都可以上锁。

_ Updated at 2016-10-03 <<< _

Requirement C

Refactor Node and MyList to ensure that insertion and removal can only be applied to nodes that belongs to the caller linked list.

Submission

Push your improved version to your own Git repo. This repo should be private and enroll us TAs as collaborators. Read the comments in the code carefully, and don’t modify the original directory structure and the class structures. However, you can rename the project root folder and add your own functions, packages, classes and data files as you like.