Home » Core Java » threads (page 3)

threads

BlockingQueue example

With this example we are going to demonstrate how to use a BlockingQueue. The steps of the example are described in short: We have created two classes that implement the Runnable interface and override its run() API method. The first runnable, FileEnumerationTask has a BlockingQueue of Files, a File that is an empty dump file and another File. It also ...

Read More »

Unbounded work queue example

This is an example of an unbounded work queue. The steps of creating and using an unbounded work queue are described below: We have created a class, WorkQueue that has a LinkedList of Objects and two methods in synchronized statement. The first one is addWork(Object o) and appends an object to the end of the list, using addLast(Object o) API ...

Read More »

Bounded work queue example

In this example we shall show you how to create a use a bounded work queue. To create and use a bounded work queue we have followed the below steps: We have created a Worker that extends the Thread and overrides its run() API method. It has a BlockingQueue of Integers and in the run() method it keeps retrieving and ...

Read More »

Sleep Thread

With this example we are going to demonstrate how to make a Thread sleep. In short, to make a Thread sleep you should: Call sleep(long millis) API method of Thread. The method causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. In the ...

Read More »

Set Thread Name

This is an example of how to set a Thread‘s name. Setting a Thread’s name implies that you should: Get a reference to the currently executing thread object, using currentThread() API method of Thread. Get the thread’s name, using getName() API method of Thread. Change the name of the thread to be equal to a specified name, using setName(String name). Get ...

Read More »

Current Thread Information

In this example we shall show you how to get the current Thread‘s information, such as id, name group and priority. To get the current Thread’s information one should perform the following steps: Get a reference to the currently executing thread, using currentThread() API method of Thread. Get the identifier of this Thread, its name, the thread group to which ...

Read More »

New Thread with Runnable

With this example we are going to demonstrate how to create a Thread with a Runnable. We have created a Thread with a Runnable as described below: We have created ThreadWithRunnableExample that implements Runnable and overrides its run() API method. In this method the Runnable’s thread sleeps using sleep(long millis) API method of Thread. We create a new Thread with ...

Read More »