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.
Be careful with multi-threading.
Just an example:
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.