Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8487531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:16:19+00:00 2026-06-10T21:16:19+00:00

I am trying to understand threads in Java. As an exercise, I created an

  • 0

I am trying to understand threads in Java. As an exercise, I created an Ice Cream class as follows.

public class ThreadIceCream {

private String flavor = "";
private String[] specialFlavors = { "Vanilla", "Chocolate", "Butter Pecan", "Strawberry", "Chocolate Chip", "Cherry", "Coffee" };
    // Constructor for ThreadIceCream class
public ThreadIceCream() {       
    int randInt = (int) (Math.random() * specialFlavors.length);
    flavor = specialFlavors[randInt];
    System.out.println("Enjoy your " + flavor + " IceCream!");
} }

The ThreadIceCream class is a simple class that creates an IceCream object with a random flavor every time the class is initialized. Here is the TestStub I am using.

public class TestStub {

public static void main(String[] args) {

    ThreadIceCream Th1 = new ThreadIceCream();
    ThreadIceCream Th2 = new ThreadIceCream();

} }

Now I want to create 10 Icecreams (i.e. Create 10 instances of the ThreadIceCream class simultaneously) and I want to use threads in Java to do this. I tried a few things but they were no were close.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:16:20+00:00Added an answer on June 10, 2026 at 9:16 pm

    Well it’s not really that hard:

    Thread[] threads = new Thread[10];
    for(int i = 0; i < 10; i++) {
        threads[i] = new Thread(new Runnable() {
                        public void run() {
                            ThreadIceCream tic = new ThreadIceCream();
                        }
                     });
        threads[i].start();
    }
    
    for(int i = 0; i < 10; i++) {
        threads[i].join();
    }
    

    Sure, this won’t do much because the work performed by each thread is so small that the overhead to start the threads is actually higher, but whatever.

    You should also learn to use the ExecutorService for higher efficiency. Pure threads are heavyweight and are rarely a good solution for anything, especially in groups. Here’s an ExecutorService version of the above:

    ExecutorService exec = Executors.newFixedThreadPool(10);
    for(int i = 0; i < 10; i++) {
        exec.submit(new Runnable() {
                        public void run() {
                            ThreadIceCream tic = new ThreadIceCream();
                        }
                    });        
    }
    
    exec.shutdown();
    exec.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
    

    Here we are creating a pool of 10 threads and submitting 10 tasks. The threads are recycled betweeen task executions, so only 10 threads are ever created, no matter how many tasks you submit. Since the tasks are so small several tasks may even be executed on the same thread, but that’s actually a good thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand how threads work in Java and currently investigating how to
Trying to understand something. I created a d:\svn\repository on my server. I committed folders
Im trying to understand how class generics work and this bit just doesnt make
I have the following code: String table; private DB.ConnectMAS mas=new DB.ConnectMAS(); java.sql.ResultSet rs1 =
I'm trying to understand the IntentService class so that I can build my own
I'm trying to understand the difference between Ruby threads pre-1.9 and 1.9 (in the
I am trying to use a priority queue (java.util.PriorityQueue) on a custom class. I
I am new to thread programming in Java. To understand threading I'm trying to
I have been going through the java.util.concurrent package and trying to understand what the
I'm trying import CSV file to Arraylist using StringTokenizer : public class Test {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.