943,769 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 965
  • Java RSS
Jun 13th, 2009
0

why is this simple code using 100% CPU?

Expand Post »
I just inctance 5 JFrames with string drawed and its using 100% ...???
that add(panel); make the diference in CPU 5 or 100%

I marked it in the comment in the code

here's the code:

Java Syntax (Toggle Plain Text)
  1. //import java.awt.event.WindowEvent;
  2. //import java.awt.event.WindowListener;
  3. import java.awt.*;
  4.  
  5. //import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8. import java.awt.event.*;
  9.  
  10. import javax.swing.*;
  11. //import javax.swing.JPanel;
  12.  
  13. import java.util.Timer;
  14. import java.util.TimerTask;
  15.  
  16.  
  17. public class WinLsnrExp extends JFrame {
  18.  
  19. private boolean a;
  20.  
  21.  
  22. public WinLsnrExp() {
  23.  
  24.  
  25. setTitle("Suncica2222");
  26. setSize(150, 150);
  27. KlasaPanel panel = new KlasaPanel();
  28.  
  29. //Container panel = getContentPane();
  30. panel.setBackground(Color.green);
  31. add(panel); // IF I COMMENT THIS LINE IT GETS NORMAL ON 5% CPU BUT ITS WHITHOUT STRING DRAWED!!!!!!!!!!!!!!!!!
  32. //repaint();
  33. panel.repaint();
  34.  
  35.  
  36.  
  37. //panel1.setBackground(Color.red);
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. }
  46. }
  47.  
  48. class BrojiKlikove
  49. {
  50. public static int a=0;
  51. public BrojiKlikove()
  52. {
  53. a++;
  54. System.out.println(a);
  55. for (int i=0;i<a-4;i++){
  56. CekajSekundi y= new CekajSekundi(0);
  57. KlasaKojaSlusa x=new KlasaKojaSlusa();
  58. }
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65. class KlasaKojaSlusa extends JFrame
  66. {
  67. private static boolean b;//nicemu ne sluzi
  68.  
  69. public KlasaKojaSlusa(String s){}
  70.  
  71. public KlasaKojaSlusa()
  72. {
  73. WinLsnrExp wle = new WinLsnrExp();
  74.  
  75. //wle.setTitle("Suncica2222 ");
  76. //wle.setSize(150, 150);
  77. //wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  78.  
  79. wle.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  80. wle.setLocation((int)(Math.random()*900),(int)(Math.random()*750));
  81. wle.setVisible(true);
  82.  
  83.  
  84.  
  85. wle.addWindowListener(new WindowAdapter()
  86. {
  87.  
  88. public void windowClosing(WindowEvent arg0) {
  89. System.out.println("Window Closing");
  90. //this.b=true; // a cannot be resolved or is not a field
  91. BrojiKlikove x=new BrojiKlikove();
  92. }
  93. });
  94. }// konstrukt
  95. }
  96.  
  97. class KlasaPanel extends JPanel
  98. {
  99.  
  100.  
  101.  
  102. public void paintComponent(Graphics g)
  103. {
  104. super.paintComponent(g);
  105. //g.Font(50);
  106.  
  107. Font font = new Font("Serif", Font.PLAIN, 100);
  108. setFont(font);
  109. //g.setColor(Color.red);
  110. g.drawString("xD",10, 90);
  111.  
  112. }
  113.  
  114.  
  115. }
Last edited by suncica2222; Jun 13th, 2009 at 3:16 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
suncica2222 is offline Offline
105 posts
since Jun 2009
Jun 13th, 2009
-1

Re: why is this simple code using 100% CPU?

Would be helpful if you can post also CekajSekundi class to see whole behaviour of your program.

Edit: Never mind above class I commented out the line where it is used. Can you post class with main method?
Last edited by peter_budo; Jun 13th, 2009 at 3:31 pm.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jun 13th, 2009
0

Re: why is this simple code using 100% CPU?

offcourse

those 2 classes are very simple


Java Syntax (Toggle Plain Text)
  1. public class KlasaZaMain
  2. {
  3.  
  4.  
  5.  
  6. public static void main(String[] args) throws Exception
  7. {
  8.  
  9.  
  10. for (int i=0;i<5;i++){
  11. KlasaKojaSlusa x=new KlasaKojaSlusa();
  12. CekajSekundi y= new CekajSekundi(0);
  13. //x.praviProzor();
  14.  
  15.  
  16. //y=new KlasaKojaSlusa("xaxaxa");
  17. //KlasaKojaSlusa.brojiKlikove();
  18. }
  19. /*while(true){
  20.  
  21.   Thread.sleep(500);
  22.   }*/
  23.  
  24. //if (KlasaKojaSlusa x==null)
  25. System.out.println(BrojiKlikove.a + "izbrojao sam");
  26.  
  27. }
  28.  
  29. }//klasa


Java Syntax (Toggle Plain Text)
  1. public class CekajSekundi {
  2. public CekajSekundi(int sekunde) {
  3. try
  4. {
  5. //wait(sekunde*1000);
  6. Thread.sleep(sekunde*1000);
  7. }
  8. catch (Exception e) {}
  9. }
  10. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
suncica2222 is offline Offline
105 posts
since Jun 2009
Jun 13th, 2009
-1

Re: why is this simple code using 100% CPU?

I believe the whole situation is created because of this random idea of handling Threads. Maybe somebody else can help you. However you may try to explain what you trying to achieve...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jun 13th, 2009
0

Re: why is this simple code using 100% CPU?

it pop ups 5 widows when started

when it's set to draw green background and "xD" string on the panel it uses 100% CPU

when it's set to blank windows it's ok and uses 5% or less

you can copy that code in to eclipse and see it for yourself,press F11

it's drawing that mess it up
Reputation Points: 10
Solved Threads: 0
Junior Poster
suncica2222 is offline Offline
105 posts
since Jun 2009
Jun 13th, 2009
-1

Re: why is this simple code using 100% CPU?

I know what it does I actually took time to try it out. I do not understand purpose behind that program of what you trying to achieve. Are you trying to learn about threads? What you been trying to achieve when you code it?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jun 14th, 2009
0

Re: why is this simple code using 100% CPU?

the program should be a lamer application that busts balls of user with popping windows from time to time

the only input parameter that we have is the listener on the red X closing button

the idea is that more is user trying to close every one of them,the more it pops out

so I have counter of clicks,I need also a timer so I could divide it to find out the speed of clicking and then calculate how much new windows it's going to pop out

so I need one class in which I'll be able to control all this and add some amount of randomness

and off course program should not be running CPU more than 3%

Java Syntax (Toggle Plain Text)
  1. //import java.awt.event.WindowEvent;
  2. //import java.awt.event.WindowListener;
  3. import java.awt.*;
  4.  
  5. //import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8. import java.awt.event.*;
  9.  
  10. import javax.swing.*;
  11. //import javax.swing.JPanel;
  12.  
  13. import java.util.Timer;
  14. import java.util.TimerTask;
  15.  
  16. //class just for frame and panel painting
  17.  
  18. public class WinLsnrExp extends JFrame {
  19.  
  20. private boolean a;
  21.  
  22.  
  23. public WinLsnrExp() {
  24.  
  25.  
  26. setTitle("Suncica2222");
  27. setSize(150, 150);
  28. KlasaPanel panel = new KlasaPanel();
  29.  
  30. //Container panel = getContentPane();
  31. panel.setBackground(Color.green);
  32. add(panel); // IF I COMMENT THIS LINE IT GETS NORMAL ON 5% CPU BUT ITS WHITHOUT STRING DRAWED!!!!!!!!!!!!!!!!!
  33. //repaint();
  34. panel.repaint();
  35.  
  36.  
  37.  
  38. //panel1.setBackground(Color.red);
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. }
  47. }
  48.  
  49. //class that counts clicks on red X closing button of the window
  50.  
  51. class BrojiKlikove
  52. {
  53. public static int a=0;
  54. public BrojiKlikove()
  55. {
  56. a++;
  57. System.out.println(a);
  58. for (int i=0;i<a-4;i++){
  59. CekajSekundi y= new CekajSekundi(0);
  60. KlasaKojaSlusa x=new KlasaKojaSlusa();
  61. }
  62. }
  63. }
  64.  
  65.  
  66. //class that makes the window frame and has a listener on red X //closing button
  67.  
  68. class KlasaKojaSlusa extends JFrame
  69. {
  70. private static boolean b;//nicemu ne sluzi
  71.  
  72. public KlasaKojaSlusa(String s){}
  73.  
  74. public KlasaKojaSlusa()
  75. {
  76. WinLsnrExp wle = new WinLsnrExp();
  77.  
  78. //wle.setTitle("Suncica2222 ");
  79. //wle.setSize(150, 150);
  80. //wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  81.  
  82. wle.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  83. wle.setLocation((int)(Math.random()*900),(int)(Math.random()*750));
  84. wle.setVisible(true);
  85.  
  86.  
  87.  
  88. wle.addWindowListener(new WindowAdapter()
  89. {
  90.  
  91. public void windowClosing(WindowEvent arg0) {
  92. System.out.println("Window Closing");
  93. //this.b=true; // a cannot be resolved or is not a field
  94. BrojiKlikove x=new BrojiKlikove();
  95. }
  96. });
  97. }// konstrukt
  98. }
  99.  
  100. //class for drawing "xD" string on panel
  101. //I would also like to draw some more strings but it wont let me...
  102.  
  103. class KlasaPanel extends JPanel
  104. {
  105.  
  106.  
  107.  
  108. public void paintComponent(Graphics g)
  109. {
  110. super.paintComponent(g);
  111. //g.Font(50);
  112.  
  113. Font font = new Font("Serif", Font.PLAIN, 100);
  114. setFont(font);
  115. //g.setColor(Color.red);
  116. g.drawString("xD",10, 90);
  117.  
  118. }
  119.  
  120.  
  121. }


class just for calling everything in main()



Java Syntax (Toggle Plain Text)
  1. public class KlasaZaMain
  2. {
  3.  
  4.  
  5.  
  6. public static void main(String[] args) throws Exception
  7. {
  8.  
  9.  
  10. for (int i=0;i<5;i++){
  11. KlasaKojaSlusa x=new KlasaKojaSlusa();
  12. CekajSekundi y= new CekajSekundi(0);
  13. //x.praviProzor();
  14.  
  15.  
  16. //y=new KlasaKojaSlusa("xaxaxa");
  17. //KlasaKojaSlusa.brojiKlikove();
  18. }
  19. /*while(true){
  20.  
  21.   Thread.sleep(500);
  22.   }*/
  23.  
  24. //if (KlasaKojaSlusa x==null)
  25. System.out.println(BrojiKlikove.a + "izbrojao sam");
  26.  
  27. }
  28.  
  29. }//klasa

class which constructor make pause of int seconds in running program

Java Syntax (Toggle Plain Text)
  1. public class CekajSekundi {
  2. public CekajSekundi(int sekunde) {
  3. try
  4. {
  5. //wait(sekunde*1000);
  6. Thread.sleep(sekunde*1000);
  7. }
  8. catch (Exception e) {}
  9. }
  10. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
suncica2222 is offline Offline
105 posts
since Jun 2009
Jun 15th, 2009
0

Re: why is this simple code using 100% CPU?

any one?
Reputation Points: 10
Solved Threads: 0
Junior Poster
suncica2222 is offline Offline
105 posts
since Jun 2009
Jun 15th, 2009
0

Re: why is this simple code using 100% CPU?

I think I can shed some light on why people have stopped replying... People don't like that sort of application, so not many people are willing to help create one.

if I were to give a fast guess, although it's not necessarily true, I would say that it's probably not a good idea to make a thread sleep within a constructor.
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Plus or minus
Next Thread in Java Forum Timeline: Simple Chat Server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC