Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
simple console application that maintains a queue of objects. All access to the queue should be handled in a thread-safe manner. At startup, the application should prompt the user for the number of producer and consumer threads to create, defaulting to two of each. The application should then spawn the appropriate number of producer and consumer threads.

Each producer thread should sit in a loop, creating a new object each++ iteration and adding it to the queue. (This does not need to be a tight loop; otherwise the queue could grow too quickly to monitor.)

Each consumer thread should sit in a loop, attempting to remove an object from the queue on each iteration. It should write a line of text to the console each iteration that contains a time stamp, a string representation of the object, an id for the producer thread that created the object, and an id for the consumer thread that displayed the object. If the queue is empty, just a time stamp and "EMPTY" should be written to the console.

The application should run until the user hits the ENTER key. At shutdown, the application should make sure all producer and consumer threads shut down, then display any objects left in the queue, waiting for the user to hit ENTER again before closing.
Updated 24-Mar-12 10:42am
Nelek 24-Mar-12 18:22pm    
And where are you having problems? This is just the description of the task.
Sergey Alexandrovich Kryukov 24-Mar-12 18:23pm    
Not a question.
om daga 25-Mar-12 8:49am    
can you possibel sole this task

Imports System.Threading

Public Class Producer
Private buffer As Buffer
Public value As Integer

Public Sub New(ByVal sharedObject As Buffer)
buffer = sharedObject
Console.WriteLine("Enter Number")
value = Integer.Parse(Console.ReadLine)

End Sub ' New

Public Sub Produce()
Dim count As Integer

For count = 1 To value
buffer.Buffer = count

Console.WriteLine(Thread.CurrentThread.Name & _
" done producing." & vbCrLf & "Terminating " & _
Thread.CurrentThread.Name & ".")
End Sub

End Class

Public Class Consumer

Private buffer As Buffer

Public Sub New(ByVal sharedObject As Buffer)
buffer = sharedObject
End Sub ' New

Public Sub Consume()
Dim count, sum As Integer

For count = 1 To 10

sum += buffer.Buffer

Console.WriteLine(Thread.CurrentThread.Name & _
" read values totaling: " & sum & "." & vbCrLf & _
"Terminating " & Thread.CurrentThread.Name & ".")
End Sub

End Class

Public Class Buffer
Private mBuffer As Integer = -1
Private occupiedBufferCount As Integer

Public Property Buffer() As Integer
If occupiedBufferCount = 0 Then
Console.WriteLine("Buffer empty. " & _
Thread.CurrentThread.Name & " waits.")

End If

occupiedBufferCount -= 1

Console.WriteLine(Thread.CurrentThread.Name & " reads " & _

Dim bufferCopy As Integer = mBuffer


Return bufferCopy
End Get

Set(ByVal Value As Integer)

If occupiedBufferCount = 1 Then
Console.WriteLine("Buffer full. " & _
Thread.CurrentThread.Name & " waits.")

End If

mBuffer = Value

occupiedBufferCount += 1

Console.WriteLine(Thread.CurrentThread.Name & " writes " & _

End Set

End Property ' Buffer

End Class
Module Tester
Sub Main()
Dim holdInteger As New Buffer()
Dim producer As New Producer(holdInteger)
Dim consumer As New Consumer(holdInteger)

Dim producerThread As New Thread(AddressOf producer.Produce)
Dim consumerThread As New Thread(AddressOf consumer.Consume)

producerThread.Name = "Producer"
consumerThread.Name = "Consumer"

End Sub

End Module

1 solution

This looks like homework.

It would be best if you did your own homework. It is given to you so that you will learn something, think about what have been taught. Read your text books and give it a try.
Have a look at this wiki: Producer-consumer problem[^]

Once you have code and run into problems you can always come back here with a more specific question and the community will do its best to help you. Don't forget to post only the relevant code bits that pose the problem as a code dump is not usually helpful at all in getting someone to help/assist you.
Share this answer
om daga 25-Mar-12 8:52am    
i was trying can u possible sole this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900