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

  • SEARCH
  • Home
  • 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 8303255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:28:52+00:00 2026-06-08T17:28:52+00:00

I am trying to learn multithreading concepts in Java. I am stuck with error

  • 0

I am trying to learn multithreading concepts in Java. I am stuck with error during execution of my code snippet.

My code snippet:

class ThreadDemo {
public static void main(String args[]) {
   try{

   int [] arr = new int[10] ;
      for(int i = 0 ; i < 10 ; i++)
         arr[i] = i ;

   for(int c =0 ; c < 2 ; c++){
         for(int i = 0 ; i < 3 ; i++) //I want to run it on one thread
             System.out.println(arr[i]);
         for(int j = 0 ; j < 5 ; j++)  //I want to run it on another thread
             System.out.println(arr[j]);
            }

} catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
   }
}

Now, to solve this I have tried,

class ThreadDemo {
public static void main(String args[]) {
try{

int [] arr = new int[10] ;
for(int i = 0 ; i < 10 ; i++)
arr[i] = i ;

    for(int c =0 ; c < 2 ; c++){
         Thread thread1 = new Thread () {
         public void run () {
         for(int i = 0 ; i < 3 ; i++) //I want to run it on one thread
           System.out.println(arr[i]);}
          };

          Thread thread2 = new Thread () {
          public void run () {
          for(int j = 0 ; j < 5 ; j++) //I want to run it on one thread
           System.out.println(arr[j]);}
          };


         }

} catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
   }
}

But gives error. Can anyone help me how to solve this ?

  • 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-08T17:28:54+00:00Added an answer on June 8, 2026 at 5:28 pm

    In

    for (int c = 0; c < 2; c++) {
        Thread thread1 = new Thread() {//<- here
    

    you are creating anonymous inner class that extends Thread class. You must know, that anonymous inner classes have access only to final local variables of method that they are created so if you want to gain access to int [] arr you must make it final like

    final int[] arr = new int[10];
    

    Also you created threads but you didn’t start them. To do that invoke their start() method like thread1.start().


    If you don’t want to declare local variables of method as final you should consider creating threads not as anonymous inner classes but as separate classes for example

    class MyThread extends Thread {
        int[] array;
        int iterations;
    
        public MyThread(int[] arr, int i) {
            array=arr;
            iterations = i;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < iterations; i++)
                System.out.println(array[i]);
        }
    }
    
    class ThreadDemo {
        public static void main(String args[]) {
            try {
    
                int[] arr = new int[10];
                for (int i = 0; i < 10; i++)
                    arr[i] = i;
    
                for (int c = 0; c < 2; c++) {
                    MyThread thread1 = new MyThread(arr, 3);
                    MyThread thread2 = new MyThread(arr, 5);
                    thread1.start();
                    thread2.start();
                }
    
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to learn multithreading and i'm stuck on this sample Imports System.Threading Public
Im trying to learn class-based views, for a detail or list view is not
While trying to learn Unity , I keep seeing the following code for overriding
Trying to learn jquery here so I took a regular javascript snippet that loops
Trying to learn R and am stuck on an autocorrelation example. I want to
Im trying to learn alot more about C code by trying to do the
I'm trying to learn on multithreading, and I have a simple question. On most
I'm trying to learn on C multithreading, and I've seen a couple of rare
trying to learn windows programming in java, want to display a image to a
I'm trying learn Python (3 to be more specific) and I'm getting this error:

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.