I’m supposed to assign a unique number to each producer and consumer thread. How do I assign a unique number to producer and consumer threads?
Add an instance (non-static) variable to the Producer/Consumer classes. When you initialize the new Producer/Consumer Objects, pass in the unique number. You can keep track of what number you're on with an int counter
in your main class.
2) The producer thread operates in an infinite loop. It produces a data item (a string) with the following format: < producer number >_< data item number > . For example the 1st data item from thread number 1 will be 1_1 and second data item from thread number 3 will be 3_2. How do create data items in such a format?
Use synchronized methods and/or atomic variables. Look into Java Concurrency.
3) Then the Producer thread writes an entry into the producer log file (< producer number > “Generated” < data item >). Upon writing the log entry, it attempts to insert into the buffer. If insertion is successful, it creates an entry into the log file (< producer number > < data item > “Insertion successful”). How do I write such a code?
My answer is the same as the previous question: read about Java concurrency. Spend an hour reading about synchronization, locks, and atomic variables and I guarantee you will easily write your program.