Loading topic
main method. This chapter shows you how to add more threads yourself.6 min readjoin, and paused them with sleep. Each thread did its own work. Real programs often need threads to work on the same data, such as a shared counter, a list of orders, or a bank balance. This is where many concurrency bugs come from.7 min readnew Thread(...). You started it, joined it, and then it was gone. That works for one or two threads. It gets hard when a program has hundreds of small jobs to run.6 min readExecutorService. Those tasks were Runnable objects. A Runnable does its job but cannot give anything back. Real programs often need an answer from a task, such as a price, a file size, or the result of a calculation.5 min readFuture. One job is left: telling the pool you are finished with it. This chapter shows why that step matters, which methods do it, and the standard pattern most Java programs use.5 min readnull. In a normal program, an exception moves up the call stack until something catches it. A task in an ExecutorService runs on a different thread, so the exception cannot mo5 min readExecutorService one at a time with submit. Each call gave you back one Future. That works well, but real programs often have a whole batch of tasks. You might need to load ten files, ask three servers for the same data, or add up the results of many small cal5 min readThread.sleep, but that ties up a thread and is easy to get wrong. Java gives you a better tool for this job: the `ScheduledExecutorS6 min readExecutors.newFixedThreadPool(4). Those methods are handy, but each one picks its settings for you. Behind every one of them is the same class, ThreadPoolExecutor. In this chapter you will build a ThreadPoolExecutor yourself and choose5 min readFuture to get a result back from a task. A Future has one weakness. The only way to use its result is to call get(), and get() makes your thread stop and wait. If you want to do something with the result, such as double it, format it, or save it, you have t6 min read