IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

Petit programme qui pompe 50% de CPU


Sujet :

Langage Java

  1. #1
    Invité
    Invité(e)
    Par défaut Petit programme qui pompe 50% de CPU
    Bonsoir,

    Alors je ne vais pas vous mettre tout le code de mon programme car d'un je pense que c'est trop long pour mettre dans ce topic et de 2 vous y passeriez des heures à comprendre ce que je fais tellement je code mal...

    BOn sinon c'est juste pour savoir si vous avez une idée de ce qui pourrais prendre autant de cpu. Le prog marche bien (du moment que je n'essaie pas de provoquer d'erreur) et il consomme toujours environ 50% de CPU.
    En fait c'est une espèce de chat et même lorsque la connexion est établie et que le client attend l'identification, et bien même là il me bouffe 50% de CPU. Alors c'est vrai que si je ne lance qu'une fois le prog ça passe... mais si je le lance 2 fois c'est INVIVABLE.

    Une idée ?

  2. #2
    Expert éminent

    Profil pro
    Fabricant et casseur d'avions
    Inscrit en
    Avril 2004
    Messages
    3 815
    Détails du profil
    Informations personnelles :
    Localisation : France, Tarn (Midi Pyrénées)

    Informations professionnelles :
    Activité : Fabricant et casseur d'avions
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Avril 2004
    Messages : 3 815
    Points : 7 644
    Points
    7 644
    Par défaut
    Salut,

    Citation Envoyé par nicofromChina
    Une idée ?
    Ben à part:

    Citation Envoyé par nicofromChina
    tellement je code mal


    Plus sérieusement, comment veux-tu qu'on t'aide sans infos???
    Revois tes algorithmes, doit y avoir des boucles pour rien quelque part...

  3. #3
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 860
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 860
    Points : 22 898
    Points
    22 898
    Billets dans le blog
    51
    Par défaut
    C'est clair, comment veux-tu qu'on t'aide si tu ne nous montres aucun code source ?

  4. #4
    Invité
    Invité(e)
    Par défaut
    Ok alors je vous mets 2 de mes classes qui je pense vous aideront à mieux comprendre.
    Bonne chance :p et merci

    Class Client (celle où il y a le main)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    package client;
     
     
    import java.net.*;
    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.io.*;
    import java.lang.Thread;
     
    import javax.swing.*;
     
    import message.*;
     
     
    public class Client extends Thread{
     
    	private BufferedReader in;
    	private PrintWriter out;
    	public String topicChatRoom;
    	public String nickname;
    	public String password;
    	private Headers header = new Headers();
    	private Login winLogin = new Login();
    	private NewUser winUser = new NewUser();
    	private CreateCroom winCreateCroom = new CreateCroom();
    	private SelectCroom winSelectCroom = new SelectCroom();
    	private ChatClient winChatClient = new ChatClient();
     
    	public Client(String ip)
    	{
    		try
    		{
    			Socket socketClient = new Socket(ip, 31311); //create a socket with the IP specified by the client and the port 31311
    			in = new BufferedReader(new InputStreamReader(socketClient.getInputStream())); //we create a new buffer  
    			 //in reading mode throw the server
    			out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socketClient.getOutputStream())),true); //same but in writing mode
    	      	this.start(); //we start the multithread
     
    		}		
    		catch( IOException e ) 
    		{
    			JFrame newFrame = new JFrame("Error");
    			WindowListener stop = new WindowAdapter() {
    				public void windowClosing(WindowEvent e){ System.exit(0);}
    		    	};
    		    newFrame.addWindowListener(stop);
     
    		    newFrame.setSize(250, 100);
    		    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    		    newFrame.setLocation(dim.width/2 - newFrame.getWidth()/2, dim.height/2 - newFrame.getHeight()/2);
    		    newFrame.setFocusable(true);
    		    JPanel jPanel = new JPanel();
    		    JLabel jLabel = new JLabel();
    		    jLabel.setBounds(new java.awt.Rectangle(8,33,66,21));
    		    jLabel.setText("Connection failed");
    		    JButton quit = new JButton("Quit");
    		    quit.setBounds(new Rectangle(53,70,107,21));
    		    quit.addMouseListener(new MouseAdapter() {
    				public void mouseClicked(MouseEvent e) {
    					System.exit(0); 
    					}
    				});
    		    quit.addKeyListener(new KeyAdapter() {
    				public void KeyPressed(KeyEvent e) {
    					if (e.getKeyCode()== KeyEvent.VK_ENTER)
    						System.exit(0);
    					}
    				});
    		    jPanel.add(jLabel);
    		    jPanel.add(quit);
    		    newFrame.setContentPane(jPanel);
    			newFrame.setVisible(true);
    			newFrame.setAlwaysOnTop(true);
     
    			System.out.println("Error client : " + e.getMessage());
    		}
    	}
     
    	public void run()
    	{
    		int i=0;
    		String message;
     
    		while(i==0) {
    			try	{
    				message =in.readLine();
    				analyseMsgs(message);
    			}catch( IOException e )	{
    				System.out.println("Error client : " + e.getMessage());
    				winChatClient.dispose();
    				break;
    			}
    		}
    	}
     
    	public void analyseMsgs(String msgReceived)
    	{
     
      		int numberOfFields;
      		int idMsg;
      		int msgType;
    		int i;
     
      		Messages msgProcessed = new Messages(msgReceived);
     
      		System.out.println(msgProcessed);
      		//we proceed depending on the message
       		switch(header.getIdCommand(msgProcessed.getField(1)))
       		{
       			case Headers.ID_TEXT:
       				System.out.println("ID_TEXT");
       				System.out.println(msgProcessed.getField(2));
       				winChatClient.jTextArea.append(msgProcessed.getField(2)+"\n");
    			break;
     
    			case Headers.ID_IDENTIFICATION_OK:
    					System.out.println(msgProcessed.getField(2));
    					winUser.clickOnOk=true;
     	  		break;
     
    			case Headers.ID_REGISTRATION_TODO:
    				winUser.jLabelText.setText("Do you want to create user \n " + nickname);
    				winUser.show();
    				// while the user didn't press "ok" or "cancel", the loop won't stop
    				while(!winUser.clickOnCancel && !winUser.clickOnOk){}
    				if(winUser.clickOnCancel) // we delete the window if the user clicks on "cancel"
    				{
    					winUser.dispose();
    				}
    				else if (winUser.clickOnOk) // if the user press "ok" we start the registration
    				{
    					System.out.println("clickOnOK");
    					registration(nickname,password);
    					winUser.dispose();
    				}
    	  		break;
     
    			case Headers.ID_CROOM_OK:
    				System.out.println(msgProcessed.getField(2));
    				topicChatRoom=msgProcessed.getField(2);
    				//winchatclien.jTextArea.append(msg_traite.getField(2)+"\n");
     
    				break;
     
    			case Headers.ID_LIST_CROOM:
    				int nbcroom=Integer.parseInt(msgProcessed.getField(2));
    				for(i=0;i<nbcroom;i++)
    				{
    					winSelectCroom.listModel.addElement(msgProcessed.getField(3+i));
    				}
    				winSelectCroom.jList.setModel(winSelectCroom.listModel);
     
    	  		break;
     
    			case Headers.ID_LIST_USERS:
    				int nbOfUsers=Integer.parseInt(msgProcessed.getField(2));
    				winChatClient.listModel.clear();
    				for(i=0;i<nbOfUsers;i++)
    				{
    					winChatClient.listModel.addElement(msgProcessed.getField(3+i));
    				}
    				winChatClient.jList.setModel(winChatClient.listModel);
     
    	  		break;
     
        		default:
        		break;
       		}
      	}
     
    	public void registration(String login,String passwd)
    	{
    		out.println("#REGISTRATION#"+login+"#"+passwd+"#");
    	}
     
    	public void identification(String login,String passwd)
    	{
    		out.println("#IDENTIFICATION#"+login+"#"+passwd+"#");
    	}
     
    	public void sendTextMessage(String text)
    	{
    		out.println("#TEXT#"+text+"#");
    	}
     
    	public void joinChatRoom(String topic)
    	{
    		out.println("#CROOM_JOINED#"+topic+"#");
    	}
     
    	public void createChatRoom(String topic)
    	{
    		out.println("#CROOM_CREATED#"+topic+"#");
    	}
     
    	public void getRoomList()
    	{
    		out.println("#LIST_CROOM#");
     
    	}
     
    	public void askToQuit()
    	{
    		out.println("#END#");
     
    	}
     
    	public static void main(String[] args)
    	{
    		Client c = new Client("127.0.0.1");
    		c.winLogin.show();
     
    		while(true){ // login window
     
    			if(c.winLogin.clickOnLogIn) // flag for the user press "login"
    			{
    				c.nickname=c.winLogin.jTextFieldLogin.getText();
    				c.password=c.winLogin.jPasswordField.getText();
    				c.identification(c.nickname,c.password); // ask identification with nickname and password parameters
    				c.winLogin.clickOnLogIn=false;
    			}
     
    			if(c.winUser.clickOnOk) // if the user is the identification is ok, we display the chat rooms list
    			{
    				c.winLogin.dispose();
    				break;
    			}			
    		}
    		c.getRoomList();
     
    		c.winSelectCroom.show();
    		while(true)
    		{
    			if(c.winSelectCroom.clickOnCreate) // if we press "create"
    			{
    				c.winCreateCroom.show();
    				while(!c.winCreateCroom.clickOnOk){}
    				c.createChatRoom(c.winCreateCroom.jTextField.getText());
    				break;
    			}
    			else if (c.winSelectCroom.clickOnJoin) // if we press "join"
    			{
    				System.out.println(c.winSelectCroom.jList.getSelectedValue().toString());
    				c.joinChatRoom(c.winSelectCroom.jList.getSelectedValue().toString());
    				break;
    			}
    		}
    		c.winSelectCroom.dispose(); // we delete windows
    		c.winCreateCroom.dispose();
     
    		c.winChatClient.show();
    		c.winChatClient.setTitle(c.topicChatRoom);
     
     
    		while(true)
    		{
    			if(c.winChatClient.typeEnter)
    			{
    				c.sendTextMessage(c.winChatClient.jTextField.getText());
    				c.winChatClient.typeEnter=false;
    				c.winChatClient.jTextField.setText("");
    				c.winChatClient.jTextField.setFocusable(true);
    			}
    			else if(c.winChatClient.cliquerclose)
    			{
    				c.askToQuit();
    				c.winChatClient.dispose();
    				break;
    			}
    		}
     
    	} 
    }
    Class Login
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    package client;
     
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.IOException;
     
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JDesktopPane;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JPasswordField;
    import javax.swing.ImageIcon;
    import java.awt.Dimension;
    import java.awt.Rectangle;
     
    public class Login extends JFrame {
     
    	private JPanel jContentPane = null;
    	private JDesktopPane jDesktopPane = null;
    	private JLabel jLabelLogin = null;
    	private JLabel jLabelPassword = null;
    	private JLabel jLabelIP = null;
    	public JTextField jTextFieldLogin = null;
    	public JTextField jTextFieldIP = null;
    	public JButton jButtonLogin = null;
    	public boolean clickOnLogIn = false;
    	public JPasswordField jPasswordField = null;
    	public ImageIcon loginIcon;
     
    	public Login() {
    		super();
    		initialize();
    	}
     
    	private void initialize() {
    		WindowListener stop = new WindowAdapter() {
    			public void windowClosing(WindowEvent e){ System.exit(0);}
    	    	};
    	    addWindowListener(stop);
     
    	    loginIcon = new ImageIcon("Icons/login.gif");
    		this.setIconImage(loginIcon.getImage());
    		this.setResizable(false);
    	    this.setSize(244, 196);
    		this.setBackground(java.awt.Color.white);
    		this.setContentPane(getJContentPane());
    		this.setTitle("Log In");
    		this.setVisible(true);
    		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    	    this.setLocation(dim.width/2 - this.getWidth()/2, dim.height/2 - this.getHeight()/2);
    		}
     
    	private JPanel getJContentPane() {
    		if (jContentPane == null) 
    		{
    			jContentPane = new JPanel();
    			jContentPane.setLayout(new BorderLayout());
    			jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);
    		}
    		return jContentPane;
    	}
     
    	private JDesktopPane getJDesktopPane() {
    		if (jDesktopPane == null) 
    		{
    			jLabelLogin = new JLabel();
    			jLabelLogin.setBounds(new Rectangle(23, 23, 67, 19));
    			jLabelLogin.setText("Login");
    			jLabelPassword = new JLabel();
    			jLabelPassword.setBounds(new Rectangle(23, 51, 66, 21));
    			jLabelPassword.setText("Password");
    			jLabelIP = new JLabel();
    			jLabelIP.setBounds(new Rectangle(23, 79, 67, 19));
    			jLabelIP.setText("IP");
    			jDesktopPane = new JDesktopPane();
    			jDesktopPane.setBackground(SystemColor.control);
    			jDesktopPane.add(jLabelLogin, null);
    			jDesktopPane.add(jLabelPassword, null);
    			jDesktopPane.add(jLabelIP, null);
    			jDesktopPane.add(getJTextFieldLogin(), null);
    			jDesktopPane.add(getJPasswordField(), null);
    			jDesktopPane.add(getJTextFieldIP(), null);
    			jDesktopPane.add(getJButtonLogin(), null);
    		}
    		return jDesktopPane;
    	}
     
    	private JTextField getJTextFieldLogin() {
    		if (jTextFieldLogin == null) 
    		{
    			jTextFieldLogin = new JTextField();
    			jTextFieldLogin.setBounds(new Rectangle(101, 22, 107, 21));
    		}
    		return jTextFieldLogin;
    	}
     
    	private JTextField getJTextFieldIP() {
    		if (jTextFieldIP == null) 
    		{
    			jTextFieldIP = new JTextField();
    			jTextFieldIP.setBounds(new Rectangle(101, 78, 107, 21));
    		}
    		return jTextFieldIP;
    	}
     
    	private JPasswordField getJPasswordField() {
    		if (jPasswordField == null)
    		{
    			jPasswordField = new JPasswordField();
    			jPasswordField.setBounds(new Rectangle(101, 50, 107, 21));
    		}
    		return jPasswordField;
    	}
     
    	private JButton getJButtonLogin() {
    		if (jButtonLogin == null) 
    		{
    			jButtonLogin = new JButton();
    			jButtonLogin.setBounds(new Rectangle(77, 115, 85, 21));
    			jButtonLogin.setText("Log In");
    			jButtonLogin.addMouseListener(new MouseAdapter() {
    				public void mouseClicked(MouseEvent e) {
    					clickOnLogIn=true; 
    					}
    				});
    			jButtonLogin.addKeyListener(new KeyAdapter() {
    				public void KeyPressed(KeyEvent e) {
    					if (e.getKeyCode()== KeyEvent.VK_ENTER)
    						clickOnLogIn=true;
    					}
    				});
    		}
    		return jButtonLogin;
    	}
    }  //  @jve:decl-index=0:visual-constraint="10,10"
    SI vous avez besoin d'en voir d'autres n'hésitez pas.

  5. #5
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 860
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 860
    Points : 22 898
    Points
    22 898
    Billets dans le blog
    51
    Par défaut
    Pourrais-tu essaye d'introduire des Thread.sleep(time_in_milliseconds) dans les boucles infinies dans lesquelles ca serait pertinent, histoire d'eviter de bouffer tout le temps CPU justemement ?

  6. #6
    Invité
    Invité(e)
    Par défaut
    T'es mon héro Bouye

    Merci énormément

  7. #7
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Citation Envoyé par nicofromChina
    T'es mon héro Bouye

    Merci énormément
    Tu as résolu ton problème ?

    Si c'est le cas, n'oublie pas le bouton


+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Programme qui consomme beaucoup de CPU
    Par houssine91 dans le forum Général Java
    Réponses: 4
    Dernier message: 16/03/2013, 15h18
  2. Réponses: 9
    Dernier message: 05/03/2013, 12h07
  3. Petit programme qui trouve un caractère.
    Par tonykart13 dans le forum Général Python
    Réponses: 14
    Dernier message: 30/03/2010, 20h35
  4. Réponses: 8
    Dernier message: 04/03/2008, 23h30
  5. Petits programmes qui m'ennuie au démarrage
    Par Ganak dans le forum Windows XP
    Réponses: 1
    Dernier message: 05/02/2007, 19h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo