Re: php vs java....

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



GH wrote:
> Can you please explain Threads to me?

Well, you start with some sheep... :-)


Real Answer:

[with gross simplifiations and outright "lies" to keep things easy]

Imagine that you have a program that wants to SEEM to be doing multiple
things at once.

For example, it wants to have a background window downloading and checking
email with a nice progress bar, while in the front window, you are reading
your email, deleting the junk, writing email, etc but *NOT* waiting for
the download to finish.

So, in essence, your program has two little separate programs running
inside of it, even though it's really really only one program.

Those two little programs are called "threads"

And once you can have two threads, there ain't nothing to stop you from
having three, four, or a hundred threads...  In theory.  You're going to
run out of system resources like RAM and stuff at some point.

Now, on the plus side, that makes it possible to do certain things that
were not feasible before.  Like having two things going on "at once"

On the downside, there is some overhead for each thread just to run.  And
really really only *ONE* thread can be truly "running" on the processor at
any given time.  Multiple CPUs increase the number of threads that can
run, at least in theory, but you can only run one thread at one time in
one processor.  So a Xeon Quad could have four truly simultaneous threads
going at one time, all from the same program.

The bigger downside is that writing good threads that don't step on each
others' toes is REALLY REALLY REALLY REALLY REALLY REALLY HARD.

They add huge major complexities to your code base, because EVERY line of
code in thread A has to worry about what it *MIGHT* do that *MIGHT* screw
up the code in thread B.

So, take the number of lines in your program, times the number of lines in
your program, and that's the number of potential flaws you have to watch
out for. :-)

Threads complicate the ability to scale your application by only throwing
more hardware at it (as Rasmus just said).

So hard, that many (all?) PHP Core Team Members don't think adding threads
is a Good Idea (tm) because the benefits are outweighed by the costs for
the target audience of PHP scripters.

Other languages have other features, and the language designers have made
informed (or not) desicions to add (or not) those features.

I personally don't think PHP *needs* threads, and it would complicate far
too many things if they were added -- certainly they shouldn't just be
"on" automatically so PHP scripters can start making threads willy-nilly!
That way lies madness.  You'd have a zillion BAD scripts out there using
threads in wildly inappropriate situations.

I suppose that a 'thread' extension could be added, at least in theory...

By all means, those of you who want such a feature are free to start
coding :-)

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux