Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

display data for a thread of many threads  RSS feed

 
Qusay Jaafar
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a text field for entering a number. I have a start button and stop button. I have another button which is "Display".
User types number (e.g 101) into the text field then clicks start button, a thread starts with name thread.setName("101"); Now, the user types number (e.g 102) another thread will start when click on start button with name thread.setName("102");
Those two threads reading data changed continously (may be counter).
Clicking Display button will show the data for the selected thread (which was typed into the text field) and display it onto a display panel.
Stop button will stop the selected thread (which was typed into the text field)
I have the idea about creating a thread and wrapping it in order to work within the event dispatch thread BUT I cannot make all these ideas together.
Please, any help?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a homework assignment.

Show us what you have so far. Where are you stuck?
 
Qusay Jaafar
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really, I am working within a project for a network company. I am reading sent and received bytes from a specific port of a switch. So, I am thinking here to create a thread for each port for reading the data continuously and that is the purpose of start button. stop button will stop that thread which is typed in the text field. display button will read the thread which is typed in the text field and read the value of the bytes of that port.
I created a ThreadFactory pattern for each button which I think I don't need it for one of them.
click on start button will create a thread and I put it into a hashMap.
click on display button will search the hashMap for that thread and suppose to read the bytes for that port.
I think creating threads for each port of the switch that because of the counter for a port will reset (starts again from zero) when the bytes reached for that port 4GB. So, creating a thread for a port will keep reading the bytes.
is my approach reasonable?
and BTY, I read the last tech tip of MultiThreading in Swing
it used another approach for handling event dispatch thread and that by using Timer class.
 
Qusay Jaafar
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just read a reply for a message. The last reply for this message was from Ernest which was:
(The purpose of the ThreadLocal class is indeed to create variables that are specific to a thread, but it's used for member or static variables. As has already been (correctly) stated in this thread, the local variables of a method, static or not, are unique to that single method invocation, and are never shared with any other invocation on the same or any other thread.)

do you think using ThreadLocal will solve my problem?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what problem you're having. The thing with the HashMap sounds fine -- why doesn't that work?
 
Qusay Jaafar
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code below can show you the main idea of what I am doing.
if new ActiveThread("1") starts then bytes in SwitchData will be changed. Now, if new ActiveThread("2") starts then bytes in SwitchData will be changed againd. Now, if new ActiveThread("1") called from the hashMap then bytes is something different than what it should be.

I am thinking now to use ThreadLocal instead of Thread class.

your suggestions please...

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Qusay Jaafar:
the code below can show you the main idea of what I am doing.
if new ActiveThread("1") starts then bytes in SwitchData will be changed. Now, if new ActiveThread("2") starts then bytes in SwitchData will be changed againd. Now, if new ActiveThread("1") called from the hashMap then bytes is something different than what it should be.


I don't see how this could be. Each ActiveThread has its own instance of the SwitchData class, and each SwitchData instance has its own bytes member, so there will be no interference between them -- unless in your real code, you've got some static variables.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!