Jump to content

thread --Help

- - - - -

  • Please log in to reply
9 replies to this topic

#1
Mozana

Mozana

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
What up guys, wim pls xplains this to me.
What is the effect of doing this:

Thread ScreenThread = new  {

  @Override

 public void run() {

  //maybe in here I call nethods to build up panels

 }

};

            ScreenThread.start();

what i wana understand is this thread, will it affect the mian thread in any way?
ok other guys can help too

#2
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Does that code compile?

If you start a new thread, then it will compete with the main thread for cpu cycles depending on how many cores your computer has.

#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
What do you mean by, "will it affect the main thread?"

In short, your example above won't affect the main thread at all. But that's just because you haven't written anything in there to cause it to do so.

However, you can, if you wish, have threads branch off of the main thread and then report back to the main thread of progress, results, exceptions, using notify(), Future, etc. It just depends on what you want your worker thread to do.

Now, I did notice you said something in your example of using the thread to build up panels. Are you trying to create GUI controls in this thread? If so, use SwingUtilities.invokeLater() to create your GUI controls on the Event Dispatch Thread. The reason for this is because Java expects you to do all Swing work in the Event Dispatch Thread. Here's an example of a generally accepted good practice for affecting your Swing controls from the EDT:

SwingUtilities.invokeLater(new Runnable() {

    @Override

    public void run() {

        JPanel panel = new JPanel();

        // initialize panel here.

        container.add(panel); // where 'container' is some Swing Container that you're adding panel to.

    }

});


Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#4
Mozana

Mozana

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
well what i wanted to make clear in the section of build was just to mention the fact that the thread will run for sum tym, what i mean by affect the main thread i meant that will the main thread wait for this one to finish before it continues? or the main thread will start this one then continue with its duties?
but in essence what I want to do is create a program that downloads stuff from the internet but do so quickly(of cause the internet speed is a factor but nevermind that), so the thread must start the download while the main thread does sum else, sorry for the essay

#5
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
When you have more than one thread in a program, the OS determines how much time each of the threads gets to execute. Whether one thread impacts the other will depend on the number of cores in your computer and how much processing time each thread uses.

#6
Mozana

Mozana

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
So will this approach yield the expected results, like make d application run faster?

#7
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
It depends on your computer and the OS. What multiple threads will do is prevent the blocking of one thread while it is waiting for input from causing another thread to wait. The code won't run any faster.

#8
Mozana

Mozana

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
ok thanks

#9
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Mozana said:

So will this approach yield the expected results, like make d application run faster?

Be sure to check this link out:
multithreading - Java - multithreaded code does not run faster on more cores - Stack Overflow

You can't assume your program will be any faster with an increase in threads. There are several other factors that need to be accounted for.

#10
Mozana

Mozana

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
so how do i make it faster, what are the other factors?
K so i checked that link out, thanks they offer some good explanations, but i am not sure if its something i can use now,
Thanks nevertheless




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users