Tuesday, July 18, 2006

Graphical Threads

Multithreading is so, so very easy in Java. Right. But there are some pitfalls we need to look after. There are no guarantees how threads would behave. Its fully at the mercy of the thread scheduler.


//myrun.java
//please remove the line numbers.
 1     class myrun implements Runnable {
2 String data;
3 public int prints;
4 public myrun(String s){
5 data=s;
6 prints=0;
7
8 }
9 public void run(){
10 while(true){
11 System.out.println(data+" printed: "+prints);
12 prints+=5;
13 try {
14 Thread.yield();
15 int flag=(int)(Math.random()*15);
16 if(flag==1)
17 {
18 Thread.sleep(25);
19 }
20 } catch (Exception e) {
21 // TODO
22 }
23 }
24
25 }
26 }

//GUI.java
//Shows the two thraeds which run and displays their progress graphically.
//Please remove the line numbers.
1 import java.awt.Color;
2 import java.awt.Dimension;
3 import java.awt.Graphics;
4
5 import java.awt.Point;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import java.awt.event.WindowAdapter;
10
11 import java.awt.event.WindowEvent;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import javax.swing.JFrame;
18 import javax.swing.JLabel;
19 import javax.swing.Timer;
20
21
22 public class GUI extends JLabel{
23 myrun first;
24 myrun second;
25 List<Point> firstPoints=new ArrayList<Point>();
26 List<Point> secondPoints=new ArrayList<Point>();
27 Timer timer;
28 int ticks;
29 static double xNormalise=15000;
30 public GUI(myrun first, myrun second) {
31 setPreferredSize(new Dimension(200,200));
32 this.first=first;
33 this.second=second;
34 timer=new Timer(100, new ActionListener(){
35 public void actionPerformed(ActionEvent e){
36 repaint();
37 }
38 });
39 }
40 public GUI() {
41 setPreferredSize(new Dimension(500,200));
42 this.first=new myrun("First");
43 this.second=new myrun("Second");
44 new Thread(first).start();
45 new Thread(second).start();
46 ticks=0;
47 firstPoints.add(new Point(0,0));
48 secondPoints.add(new Point(0,0));
49 timer=new Timer(100, new ActionListener(){
50 public void actionPerformed(ActionEvent e){
51
52 ticks++;
53
54 int xPos1=(int)(first.prints/xNormalise*getWidth());
55 while(xPos1>getWidth()){
56 xPos1-=getWidth();
57 }
58 int xPos2 = (int)(second.prints/xNormalise*getWidth());
59 while(xPos2>getWidth()){
60 xPos2-=getWidth();
61 }
62 int yPos=ticks;
63 firstPoints.add(new Point(xPos1,yPos));
64 secondPoints.add(new Point(xPos2,yPos));
65 repaint();
66 }
67 });
68
69 timer.start();
70
71 }
72 public void paintComponent(Graphics g){
73 super.paintComponent(g);
74 g.setColor(Color.WHITE);
75 g.fillRect(0,0,getWidth(),getHeight());
76 Point prev1;
77 Point next1;
78 Iterator<Point> i=firstPoints.iterator();
79 prev1 = i.next();
80 next1 = i.next();
81 g.setColor(Color.RED);
82 g.drawLine(prev1.x,prev1.y,next1.x,next1.y);
83 while(i.hasNext()){
84 prev1=next1;
85 next1=i.next();
86 g.drawLine(prev1.x,prev1.y,next1.x,next1.y);
87 }
88 i=secondPoints.iterator();
89 prev1 = i.next();
90 next1 = i.next();
91 g.setColor(Color.GREEN);
92 g.drawLine(prev1.x,prev1.y,next1.x,next1.y);
93 while(i.hasNext()){
94 prev1=next1;
95 next1=i.next();
96 g.drawLine(prev1.x,prev1.y,next1.x,next1.y);
97 }
98
99 }
100 public static void main(String args[]){
101 JFrame f=new JFrame();
102 f.add(new GUI());
103 f.pack();
104 f.setVisible(true);
105 f.addWindowListener(new WindowAdapter(){
106 public void windowClosing(WindowEvent e){
107 System.exit(0);
108 }
109 });
110 }
111
112 }

0 Comments:

Post a Comment

<< Home