13

Any programmers around? Which of the following is correct, or more common:

  • The code runs in a background thread.
  • The code runs on a background thread.

That's it. Just a simple word different.

As an alternative, the code might also be executed in/on a thread, if that makes a difference.

16
  • 10
    Either is correct, depending somewhat on the particular audience and computer environment.
    – Hot Licks
    Commented Jan 29, 2015 at 20:27
  • 2
    The code runs as a background thread.
    – JamesRyan
    Commented Jan 30, 2015 at 10:52
  • 1
    @JamesRyan: What nonsense is this. I feel like you are nitpicking for the sake of it. "Thread" is well-defined and the code that executes in a thread is clearly only one part of what constitutes a thread. Now you introduce this notion of "thread object" which is a meaningless distinction. I think we're done here. Commented Jan 31, 2015 at 4:14
  • 2
    @JamesRyan: Are you confusing threads with classes that encapsulate the control and management of threads? Commented Feb 1, 2015 at 17:34
  • 2
    @JamesRyan: In a CPU a thread does not exist! Threads exist only down to the layer of abstraction provided by a scheduling OS, in which they are a lot more than "a series of code". (Modern processors' ability to perform parallelisation across cores and using hyperthreading techniques is entirely orthogonal to any notion of "thread" that you will find in programming). Commented Feb 1, 2015 at 18:50

3 Answers 3

21

In a thread (but on a server or an OS), in my experience as a programmer.

7
  • 1
    I agree. Compare what you say about task, a notion allied to thread. Code can run in a task, but it would be odd to say it runs "on a task".
    – Greg Lee
    Commented Jan 29, 2015 at 20:22
  • In my experience as a programmer, in and on are used approximately equally w/r/t threads, and as is also occasionally used. Most programmers are not particularly concerned with the grammar of languages designed for inter-human communication.
    – bcrist
    Commented Jan 30, 2015 at 1:51
  • 6
    But if you were refering to the thread from somewhere else there is a big difference between "wait in a thread" and "wait on a thread"
    – mgb
    Commented Jan 30, 2015 at 4:07
  • @bcrist: I've never heard "on a thread" and it sounds very odd to me. And as for your statement regarding "most programmers" well, blimey, most crappy programmers maybe but not the sort whose particular way of going about life matches the requirements of being good at software development: rigour, precision and care. Commented Jan 30, 2015 at 17:45
  • @bcrist: as a former programmer who is now a technical writer (about 15 years of each) I can attest to the truth of your statement about most programmers' view of humanspeak. But they are careful and precise in referring to their code. Thus, it is plausible that "on a thread" and "in a thread" might have completely distinct meanings. So the question was worth asking. Granted, it wasn't really a question of English-language-usage so much as a question of how-do programmers-use-English. Commented Jan 31, 2015 at 13:44
6

I've heard/read both being used pretty much interchangeably. If you want to make a distinction, maybe look at how the code you're describing is being run.

Example 1: Given two paths of execution (two threads), and one routine is always run on the secondary thread, one might say that routine is run "on the thread".

Example 2: Given one primary thread, and a subroutine which starts its own thread to run asynchronously, then it might be said to run "in the thread".

2
  • 3
    Yep, been programming over 40 years, and I've seen both tens of thousands of times. There are contexts where one is probably preferred over the other, but the distinction would hardly ever be critical.
    – Hot Licks
    Commented Jan 29, 2015 at 20:30
  • 3
    @HotLicks The context absolutely matters. Tasks run on a thread, but code runs in a thread. The difference comes about because of the different types of abstraction-- in some cases the thread is a platform that hoists up tasks, like the programs that run on a server, while in other cases, the thread is described as a container that holds the code, being that threads are system objects, like processes, but lightweight, and fully contain the code (should it escape, the system would abort execution of the thread).
    – phyrfox
    Commented Jan 30, 2015 at 4:32
5

Use "on the thread" to mean that a thread (usually named) is going to carry out a task. For example, events can be dispatched "on the Event thread." One might also say that "graphics rendering occurs on the UI thread." You wouldn't ordinarily refer to code as running "on the thread," but instead used to describe what task runs in that context. This is roughly analogous to running something "on a server."

Use "in the thread" to mean that a thread (possibly unnamed) is running code. Threads are abstract concepts that operate like a box, and on all modern systems, code runs within that context, and are therefore "in the thread." It's usually described in a general way to conceptualize how threads work.

There is usually only one right way to refer to in/on the thread, and you'd have to use the correct context or it might sound odd to professional or experienced developers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.