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 7667063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:00:08+00:00 2026-05-31T15:00:08+00:00

I am having a problem in running two threads in parallel. Each of the

  • 0

I am having a problem in running two threads in parallel. Each of the threads run fine individually. My requirement is to display 10 balls, a red ball if value is 0 and a green ball if value is 1, one after the other. The data in value is received from an array containing 0s or 1s. I need to run 16 such threads together. I am currently trying with two.

package pkg2;
public class mainClass {

public static void main(String[] args) {
    Intermediate frame = new Intermediate();
    }
}

The main class calls the intermediate class

package pkg2;

import java.awt.Color;
import javax.swing.*;

public class Intermediate extends JFrame {

    public Intermediate() {
        DivScreen ob = new DivScreen();
        ob.setBackground(Color.black);
        ob.divScreen1(16);
        add(ob);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(1370, 740);
        setResizable(false);
    }
}

In an intermediate class, an object of DivScreen class is made where all the threading and GUI part is done.

package pkg2;

import java.awt.*;
import javax.swing.*;

public class DivScreen extends Canvas implements Runnable//,ActionListener
{

    Thread t1[];            //threads
    int i, j;               //n=total no. of lines, i=no. of rows, j=no of columns
    public static int x;    // x is now global variable
    public static int i1 = 0, i2 = 0;   //to continue fetching data from last entry
    public static int c1 = 0, c2 = 0;   // to check whether line is working or not
    public static int y1, y2;   // to show red or green balls
    public static int k1 = 0, k2 = 0;   //to draw 10 balls
    int green, blue, red;   //variables for color of lines
    int arr1[] = {1, 1, 0, 1};
    int arr2[] = {1, 0, 1, 0, 1, 0};

    public DivScreen() //default.  constructor
    {
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Font f = new Font("Arial", Font.BOLD, 30);
        setFont(f);
    }

    public void divScreen1(int m) {
        t1 = new Thread[2]; //HERE WE HAVE TO PAAS n AS SIZE OF THREAD ARRAY 
        // BUT JUST TO CHECK ITS WORKING WE ARE USING 2 THREADS 
        for (int i = 0; i < 2; i++) {
            t1[i] = new Thread(this, (i + 1) + "thread");
            t1[i].start();
        }
    }

    public void paint(Graphics g) {
        g.setColor(Color.white);

        for (int i = 1; i < 4; i++) {
            j = i;                              //j is for horizontal lines
            g.drawLine(i * 342, 0, i * 342, 740); //i is for vertical lines
            g.drawLine(0, j * 185, 1370, j * 185);
        }
        if (x == 1) {
            g.setColor(Color.GRAY);
            g.drawString("Line 1", 150, 50);
            if (c1 == 0) {
                g.drawString("Line is not in use", 30, 150);
                g.setColor(Color.black);
                g.fillRect(45, 90, 200, 30);
            } else {
                g.setColor(Color.black);
                g.fillRect(30, 120, 250, 30);
                if (k1 < 10) {
                    if (y1 == 0) {
                        g.setColor(Color.red);
                    } else {
                        g.setColor(Color.green);
                    }
                    g.fillOval(50 + 20 * (k1++), 100, 15, 15);
                } else {
                    k1 = 0;
                    g.setColor(Color.black);
                    g.fillRect(45, 90, 200, 30);
                }
            }
        }

        if (x == 2) {
            g.setColor(Color.gray);
            g.drawString("Line 2", 460, 50);
            if (c2 == 0) {
                g.drawString("Line is not in use", 370, 150);
                g.setColor(Color.black);
                g.fillRect(385, 90, 200, 30);
            } else {
                g.setColor(Color.black);
                g.fillRect(370, 120, 250, 30);
                if (k2 < 10) {
                    if (y2 == 0) {
                        g.setColor(Color.red);
                    } else {
                        g.setColor(Color.green);
                    }
                    g.fillOval(390 + 20 * (k2++), 100, 15, 15);
                } else {
                    k2 = 0;
                    g.setColor(Color.black);
                    g.fillRect(385, 90, 200, 30);
                }
            }
        }
    }

    public void update(Graphics g) {
        paint(g);
    }

    public void run() {
        while (true) {
            if (Thread.currentThread().getName().equals("1thread")) {
                x = 1;
                int value = 0;              // to get value from array              
                while (i1 < 4) {
                    c1 = 1;
                    value = arr1[i1];   //valid is a value containing 1 or 0 
                    i1++;               // 1 implies product is OK, 0 implies product not OK

                    System.out.println(value);
                    if (value == 1) {
                        y1 = 1;         // we will check its value in paint() function
                    } else {
                        y1 = 0;
                    }
                    SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            repaint(0, 0, 342, 185);
                        }
                    });


                    try {
                        Thread.sleep(200);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }

                c1 = 0;
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        repaint(0, 0, 342, 185);
                    }
                });


                try {
                    Thread.sleep(200);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }

            if (Thread.currentThread().getName().equals("2thread")) {
                x = 2;
                int value2 = 0;                 // to get value from arr2[]

                while (i2 < 6) {
                    c2 = 1;
                    value2 = arr2[i2];
                    i2++;
                    System.out.println(value2);
                    if (value2 == 1) {
                        y2 = 1;
                    } else {
                        y2 = 0;
                    }
                    SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            repaint(342, 0, 342, 185);
                        }
                    });

                    try {
                        Thread.sleep(200);
                    } catch (Exception e) {
                        System.out.println(e);
                    }

                }

                c2 = 0;
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        repaint(342, 0, 342, 185);
                    }
                });

                try {
                    Thread.sleep(200);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
    }
}

Please reply if you could find something.
Thanks.

  • 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-05-31T15:00:11+00:00Added an answer on May 31, 2026 at 3:00 pm

    Be careful with multi-threading.

    Just an example:

    • x is a static variable that is going to be changed by both threads and also used in paint(). You cannot guarantee anything on the state of x at the beginning or during the execution of paint(). The threads might just change it at will.

    I suggest that you read a little more on threads and concurrency and how you can manage it but I would say that the biggest problem is the design of the program itself. What exactly are you trying to achieve and why is multithreading needed? If you want to make extensive calculations on what to paint it is ok to have multiple threads for these calculations but the painting method should receive that information in a concurrently safe manner. Only one single thread should do the paint job and the information used during the paint should be locked.

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

Sidebar

Related Questions

I'm having a problem running a T4 template using TextTransform.exe on my build server.
I'm having a problem running a VS 2005 app on some machines and not
I'm having problem when running my Windows Forms program. In the program, I have
I'm having a problem running Apache + Subversion with SSL behind an Nginx proxy
I'm having a problem with running an application I made on another computer. This
I'm using django with app-engine-patch and I'm having this wierd problem running manage.py dumpdata
I'm having a problem with keeping a JBoss server running. Here's the command I'm
I'm having this problem on a new cruisecontrol.net install running on Windows Server 2003
I'm running ubuntu 11.10 on a vmware 4.0 player. I'm having a problem and
Recently, I've started having a problem with my SQL Server 2005 client running on

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.