Java Thread - Exercises, Practices, Solutions
Java Thread Exercises [7 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system. The implementation of threads and processes differs between operating systems.
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.
1. Write a Java program to create a basic Java thread that prints "Hello, World!" when executed.
2. Write a Java program that creates two threads to find and print even and odd numbers from 1 to 20.
3. Write a Java program that sorts an array of integers using multiple threads.
4. Write a Java program that performs matrix multiplication using multiple threads.
5. Write a Java program that calculates the sum of all prime numbers up to a given limit using multiple threads.
6. Write a Java program to implement a concurrent web crawler that crawls multiple websites simultaneously using threads.
7. Write a Java program that creates a bank account with concurrent deposits and withdrawals using threads.
Java Code Editor
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Java: Tips of the Day
IsPowerOfTwo
Checks if a value is positive power of two.
To understand how it works let's assume we made a call IsPowerOfTwo(4).
As value is greater than 0, so right side of the && operator will be evaluated.
The result of (~value + 1) is equal to value itself. ~100 + 001 => 011 + 001 => 100. This is equal to value.
The result of (value & value) is value. 100 & 100 => 100.
This will value the expression to true as value is equal to value.
public static boolean isPowerOfTwo(final int value) { return value > 0 && ((value & (~value + 1)) == value); }
Ref: https://bit.ly/3sA5d4I
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook