Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads  RSS feed

 
Nathan Doe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a project that simulates a registrars office at a college, it has three classes Student, Clerk, and RegistrarSimulator.
The RegistrarSimulator is supposed to create a user specified amount of clerks and then wait for students to get in line (which will be a ConcurrentLinkedQueue). What I am having trouble with is the Clerk class implements Runnable so it needs a run() method. inside that run method when there is a student in the line the Clerk is supposed to "help" the student and when this happens there should be a print out to the console that says "Clerk m is serving Student n" (where m is the Clerks id number and n is the Students id number). In both the Student and Clerk class I have a toString() method that will assist with printing that out, but i don't see how in the Clerk class i can use the toString() method since the Clerk and Student don't get created until the RegistrarSimulator class.

I first did:


but that obviously doesn't work... So does anyone see how i could do this?


 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan:

Well, from your description, it looks like all of your Clerks will be created when the RegistrarSimulator object is created. I'm not sure why you think that your example code 'obviously doesn't work'. Can you elaborate on why you think it won't work? It seems pretty straightforward to me.

John.
 
Campbell Ritchie
Marshal
Posts: 55725
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are attempting to call an instance method on the class name, which won't work. You don't need to say toString() because System.out.println() will look for toString() as a default. Try this:

System.out.println(clerk + " is helping " + student);

Obviously you can do that when you have a reference to Clerk and Student objects. You will find out soon enough if you forget to initialise them: it will print "null".

But what has this got to do with threads?

 
Nathan Doe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Clerk class though there is no instance of student or clerk for that matter to reference though which is where the run() method is. So how would

System.out.println(clerk + " is helping " + student);

work if there is no clerk and no student? And i entitled it Threads because i am implementing Runnable so I could use threads with Clerks. Perhaps another title would've been better though...
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan:

From what you described, this is what it sounds like your system is supposed to do:

Create an object of type RegistrarSimulator, which creates a number of objects of type Clerk (based on user input) when instantiated.
Run each Clerk as a thread (using the run() method).
Each Clerk will associate itself with a Student object (which are collected in a ConcurrentLinkedQueue object).
Each Clerk will print a message out to the console which states "Clerk m is serving Student n".

I believe what you need to think about is how you would go about associating a Student with a Clerk. Once you've figured that out, doing the toString() method becomes trivial.

John.
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does your run method have so far? Are you just in a loop, waiting to help the students? If you have any code please post it.
 
Nathan Doe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is what i have so far... but the:

System.out.println("Clerk " + num + " is serving " + s1);

does not work because when it prints num it is the last clerk created not the current clerk that is serving a student.
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where and how is your num variable initialized? you could also try replacing "num", with this (that is the java keyword 'this"), provided you have a toString() method for Clerk. However that may or may not work, depending on if your toString() method just prints out the num again. I would try it anyway
 
Campbell Ritchie
Marshal
Posts: 55725
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never write == true. You simply write if (q1.isEmpty()) . . . Writing == true looks poor style and can cause errors if you write = instead of == by mistake.

If you need == false, you don't write that either. You write if (!q1.isEmpty()) . . .

Remember the bang operator (!) has a higher precedence than */ and % so you may need additional () pairs to preserve precedences.
 
Nathan Doe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable num is initialized within the Clerk class. And I tried using "this" for it and it seemed to work. However, I seem to have come across another problem. While running the RegistrarSimulator class (which is where the main() method is for the program) it prompts the user for a number of Clerks and Students, and then is supposed to create the amount of Students add then to a Queue, and then create the number of Clerks and make a new Thread for each Clerk to serve Students. When I run the following code it runs once with an output of:

Enther the amount of Clerks
4
Enther the amount of Students
3
Student 1 entered line (1 student(s) waiting)
Clerk 2 is serving Student 1

but then it stops running. I am guessing it has something to do with how I am initializing my Threads but I cant seem to figure out what is wrong. Any help is appreciated.




 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly does "synchronized (lock)" mean (sorry threads are not my speciality) I assume that it means that only one thread can access the code inside of it at a time. If so then you have an endless loop inside of that block of code, and so the lock to that code will never be released once it has started. Also is that your entire output? if not then please post all of it.

Also why do you need two BufferedReaders, that probably doesn't effect your code at all, but I don't see why you would need to.

One more thing. You call System.exit() as soon as you finish starting all the threads. Won't that just end all the threads? You should probably wait until you are done serving all the students
 
Nathan Doe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"synchronized (lock)" is just a lock so that the thread can't get paused and then have another thread change something that would effect the other thread. I also had fixed the System.exit() but somehow the problem was in the for loop that created students i figured out... it was only creating one student and then stopping. I just retyped it and now that part works. but now only one Clerk is serving all of the Students, so if i make 2 Clerks only Clerk 2 would serve all of the students... heres the code and the output now:


Enther the amount of Clerks
2
Enther the amount of Students
6
Student 1 entered line (1 student(s) waiting)
Clerk 2 is serving Student 1
Student 2 entered line (1 student(s) waiting)
Clerk 2 is serving Student 2
Student 3 entered line (1 student(s) waiting)
Clerk 2 is serving Student 3
Student 4 entered line (1 student(s) waiting)
Clerk 2 is serving Student 4
Student 5 entered line (1 student(s) waiting)
Clerk 2 is serving Student 5
Student 6 entered line (1 student(s) waiting) -----> The last Student is also never served



 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try making your concurrentLinkedQueue volatile, if that doesn't work then move your synchronized statement to inside the endless loop, or better yet, just around the code that needs to be protected
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!