KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > adapt > SenderThread


1 package org.jgroups.tests.adapt;
2
3 import org.apache.log4j.Logger;
4 import org.jgroups.Channel;
5 import org.jgroups.ChannelClosedException;
6 import org.jgroups.ChannelNotConnectedException;
7 import org.jgroups.Message;
8
9 /** Sender thread: inputs into the system a num_busts bursts
10  * of msgs_burst messages composed of msg_size bytes.
11  * Sleeps for sleep_msec after each burst.
12  * @author Milcan Prica (prica@deei.units.it)
13  * @author Bela Ban (belaban@yahoo.com)
14
15  */

16 public class SenderThread extends Thread JavaDoc {
17
18     private Channel channel;
19     private int num_msgs;
20     private int msg_size;
21     Logger log=Logger.getLogger(this.getClass());
22     long log_interval=1000;
23     boolean gnuplot_output=Boolean.getBoolean("gnuplot_output");
24
25     public SenderThread(Channel ch, int num_msgs, int msg_size, long log_interval) {
26         channel=ch;
27         this.num_msgs=num_msgs;
28         this.msg_size=msg_size;
29         this.log_interval=log_interval;
30     }
31
32     public void run() {
33         long total_msgs=0;
34
35         System.out.println("Sender thread started...");
36
37         try {
38             byte[] msg=new byte[msg_size];
39             for(int h=0; h < msg_size; h++) {
40                 msg[h]=(byte)h;
41             }
42
43             for(int i=0; i < num_msgs; i++) {
44                 Message jg_msg=new Message(null, null, msg);
45                 channel.send(jg_msg);
46                 total_msgs++;
47                 if(total_msgs % 1000 == 0) {
48                     System.out.println("++ sent " + total_msgs);
49                 }
50                 if(total_msgs % log_interval == 0) {
51                     if(gnuplot_output == false)
52                         if(log.isInfoEnabled()) log.info(dumpStats(total_msgs));
53                 }
54             }
55             System.out.println("Sent all messages. Sender terminates.\n");
56         }
57         catch(ChannelNotConnectedException e) {
58             e.printStackTrace();
59         }
60         catch(ChannelClosedException e) {
61             e.printStackTrace();
62         }
63     }
64
65
66     String JavaDoc dumpStats(long sent_msgs) {
67         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
68         sb.append("\nmsgs_sent=").append(sent_msgs).append('\n');
69         sb.append("free_mem=").append(Runtime.getRuntime().freeMemory());
70         sb.append(" (total_mem=").append(Runtime.getRuntime().totalMemory()).append(")\n");
71         return sb.toString();
72     }
73 }
74
Popular Tags