Needs to be thread-safe
7910
private static Stream<Node> streamAttributes(Node node) {
        final NamedNodeMap attributes = node.getAttributes();
        if (attributes == null) return Stream.empty();
        return IntStream.range(0, attributes.getLength()).mapToObj(attributes::item);
    }
258346
def halt(self):
        
        if self._callback:
            self._thread_continue = False
            self._thread.join()
19607
def threadpooled(
    func: typing.Callable[..., typing.Union["typing.Awaitable[typing.Any]", typing.Any]],
    *,
    loop_getter: typing.Union[typing.Callable[..., asyncio.AbstractEventLoop], asyncio.AbstractEventLoop],
    loop_getter_need_context: bool = False,
) -> typing.Callable[..., "asyncio.Task[typing.Any]"]:
    """"""
1137529
private void checkThread() {
        if (Thread.currentThread() != thread) {
            throw new IllegalStateException("Don't cross-thread using the "
                    + Context.class.getName() + " object, it's thread-local only. context thread: "
                    + thread.getName() + ", current thread: " + Thread.currentThread().getName());
        }
    }
622458
def sync(self, expectedThreads=0):
        ''
        while len(self.unfinishedThreads) > expectedThreads:
            time.sleep(.3)
            self.checkForFinishedThreads()
1005024
def _init_threading(self, function, params={}, num_threads=10):
        
        q = Queue(maxsize=0) # TODO: find something appropriate
        for i in range(num_threads):
            worker = Thread(target=function, args=(q, params))
            worker.setDaemon(True)
            worker.start()
        return q
1009059
function(threadId, callback) {
                if (threadId) {
                    var tempSendQueue = [];

                    for (var i = 0; i < chatSendQueue.length; i++) {
                        if (chatSendQueue[i].threadId == threadId) {
                            tempSendQueue.push(chatSendQueue[i]);
                        }
                    }
                    callback && callback(tempSendQueue);
                }
                else {
                    callback && callback(chatSendQueue);
                }
            }
949123
public static void waitForDie(Thread thread) {
		boolean dead = false;
		do {
			try {
				thread.join();
				dead = true;
			} catch (InterruptedException e) {
				// ignore
			}
		} while (!dead);
	}
481435
private static <T> ThreadContext<T> getCurrentThreadContext(ThreadContext<T> threadContext)
    {
        return ((ThreadContextImpl<T>) threadContext).get();
    }
949992
public void onChildThreadLaunchEnd()
  {
    try {
      if (_threadMax <= _threadCount.getAndDecrement()) {
        wake();
      }

      wakeIfLowIdle();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
609115
public void setThreadCount(int thread) {
		this.threadCount = thread;
		getConfig().setProperty(SPIDER_THREAD, Integer.toString(this.threadCount));
	}
1319555
@Override
    public boolean isThreadSafe(Tuple<ScriptEngine, String> key, ScriptEvaluator instance) {
        final ScriptEngineFactory factory = key.first.getFactory();
        final Object threadingAbility = factory.getParameter("THREADING");
        return threadingAbility != null && ("THREAD-ISOLATED".equals(threadingAbility) || "STATELESS".equals(threadingAbility));
    }
1157407
@Override
	public Thread newThread(Runnable runnable) {
		Thread t = new Thread(group, runnable, namePrefix + threadNumber.getAndIncrement());
		t.setDaemon(true);

		t.setPriority(threadPriority);

		// optional handler for uncaught exceptions
		if (exceptionHandler != null) {
			t.setUncaughtExceptionHandler(exceptionHandler);
		}

		return t;
	}
87761
public void lock() {
		if (cancel != null)
			return;
		if (error != null)
			return;
		Thread t;
		BlockedThreadHandler blockedHandler;
		do {
			synchronized (this) {
				if (!locked) {
					locked = true;
					return;
				}
				t = Thread.currentThread();
				blockedHandler = Threading.getBlockedThreadHandler(t);
				if (blockedHandler != null) break;
				try { this.wait(0); }
				catch (InterruptedException e) { continue; }
			}
		} while (true);
		blockedHandler.blocked(this, 0);
	}
1205189
private Request bindThreadToRequest(Thread thread, EntryPoint entryPoint) {
		StandardRequest boundRequest; /*= rebindingRequest;*/
//		thread.setContextClassLoader(contextClassLoader);

		int threadId = thread.hashCode();
		synchronized (requestsByThreadId) {
			StandardRequest previouslyBoundRequest = (StandardRequest) requestsByThreadId.get(threadId);

			if (previouslyBoundRequest == null) {
				boundRequest = new StandardRequest(entryPoint, this);
				requestsByThreadId.put(threadId, boundRequest);
			} else {
				// request is already bound, it will not be overwritten
				previouslyBoundRequest.increaseTimesEntered();
				boundRequest = previouslyBoundRequest;
			}
		}
		return boundRequest;
	}
1173998
private void doInterruptAllWaitingThreads() {
    	//Interrupt all the threads
    	for (int i=0; i<nThreads; i++) {
    		threads[i].interrupt();
    	}
    	
    	synchronized(lock) {
    		lock.notify();
    	}
    }
1025649
public static boolean shouldEnable(Thread thread) {
		for (StackTraceElement element : thread.getStackTrace()) {
			if (isSkippedStackElement(element)) {
				return false;
			}
		}
		return true;
	}