Jump to content


Threads


  • Please log in to reply
2 replies to this topic

#1 kebbe

kebbe

    Member

  • Members
  • PipPip
  • 16 posts
  • Gender:Male
  • Location:Ottawa, ON

Posted 13 October 2009 - 12:51 AM

I'm having problems with threads, I sort of understand the concept however not to the full extent. If someone can explain this concept with examples and it's usage.

Greatly Appreciated,
Khaled

#2 ami

ami

    Advanced Member

  • Members
  • PipPipPip
  • 86 posts
  • Gender:Male
  • Location:Poland

Posted 12 November 2009 - 08:29 AM

Hi Khaled!

  I understand threads as a part of code which you just pack into:

CODE
  Thread t = new Thread() {
    public void run(){
      // here please place your code
    }
  }


When you're ready to execute it, just run it with t.start(). It will run concurrently to your application, in the same time.

Please try :-)

What can be done using threads?

- for example scanning ports. You've trying 65 000 times to open connection to a socket on subsequent ports and wait 1 second. Without threads it would take 18 hours. But with threads you can be checking multiple ports at the same :-) So, if you were testing 1000 ports at one time (with 1000 threads), it takes about 2 minutes. You'll notice that your computer now works harder and CPU usage increased. Well, I guess you could also run all 65 000 threads at one time, to have that job completed in just one second.

Very good tutorial on threads from Sun can be found here

Cordially, K.





QUOTE (kebbe @ Oct 13 2009, 01:51 AM) <{POST_SNAPBACK}>
I'm having problems with threads, I sort of understand the concept however not to the full extent. If someone can explain this concept with examples and it's usage.

Greatly Appreciated,
Khaled


#3 Captain Pierce

Captain Pierce

    Advanced Member

  • Moderator
  • PipPipPip
  • 877 posts
  • Gender:Male
  • Location:Georgia

Posted 12 November 2009 - 06:51 PM

Oh dear god threading is not a simple concept for a forum post or two. Whole books have been written on the topic of "concurrent programming" because it's not usually so easy to just sprinkle thread calls everywhere. Problems arise when multiple threads are executing the same section of code.

For example, something as simple as variable++ can break (it may or may not break at all, every once in a while, or frequently, depending on the machine specs) since that really is a read, modify, write combination.

You should start googling "Java Concurrent Programming" and look at various tutorials and concepts.