6

What is event-driven programming and has event-driven programming anything to do with threading? I came to this question reading about servers and how they handle user requests and manage data. If user sends request, server begins to process data and writes the state in a table. Why is that so? Does server stop processing data for that user and start to process data for another user or processing for every user is run in a different thread (multithread server)?

omegasbk
  • 826
  • 1
  • 10
  • 24
  • 1
    Seriously? Why not just google the Question title? http://en.wikipedia.org/wiki/Event-driven_programming – JonnyReeves Apr 25 '12 at 21:01
  • 3
    it doesn't give me the right answers :( – omegasbk Apr 25 '12 at 21:03
  • I'm not really sure you even have a question here. To answer your question, no; that's not what Event Driven programming refers to; you may wish to read up on the [Observer Pattern](http://en.wikipedia.org/wiki/Observer_pattern) – JonnyReeves Apr 25 '12 at 21:05
  • 3
    Funny - I googled the same question and was taken here! – Chap Jan 19 '13 at 03:27
  • @Chap haha :) I hope it helped as it helped me! – omegasbk Jan 20 '13 at 22:37
  • Actually, it has nothing to do with threading, there are quite a few concepts in event-driven design like: Event-Carried State Transfer, Event Notification, Event-Sourcing, CQRS Great intro to the event-driven application by Martin Fowler: https://martinfowler.com/articles/201701-event-driven.html Also, do watch his opening keynote at goto Chicago in April 2017. – marcin naglik May 29 '19 at 22:57

1 Answers1

7

Event driven programming != Threaded programming, but they can (and should) overlap.

Threaded programming is used when multiple actions need to be handled by a system "simultaneously." I use simultaneously loosely as most OS's use a time sharing model for threaded activity, or at least they do when there are more threads than processors available. Either way, not germane to your Q.

I would use threaded programming when I need an application to do two or more things - like receiving user input from a keyboard (thread 1) and running calculations based upon the received input (thread 2).

Event driven programming is a little different, but in order for it to scale, it must utilize threaded programming. I could have a single thread that waits for an event / interrupt and then processes things on the event's occurrence. If it were truly single threaded, any additional events coming in would be blocked or lost while the first event was being processed. If I had a multi-threaded event processing model then additional threads would be spun up as events came in. I'm glossing over the producer / worker mechanisms required, but again, not germane to the level of your question.

Why does a server start processing / storing state information when an event is received? Well, because it was programmed to. :-) State handling may or may not be related to the event processing. State handling is a separate subject from event processing, just like events are different than threads.

That should answer all of the questions you raised. Jonny's first comment / point is worth heeding - being more specific about what you don't understand will get you better answers.

  • "any additional events coming in would be blocked or lost while the first event was being processed" So is this what happens with Node.js since it is single-threaded? – Donato Jul 11 '15 at 17:57
  • @Donato - I don't know enough about node.js to answer that question. With a bit more detail, it might make a good enough SO question of its own. –  Jul 11 '15 at 20:28