Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 2.49 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Multiple threads in Java
  2. public class agent extends Thread {
  3.  
  4.     private table smokingtable;
  5.  
  6.     public agent(table pSmokingtable)
  7.     {
  8.         smokingtable = pSmokingtable;
  9.     }
  10.  
  11.     @Override
  12.     public void run()
  13.     {
  14.         while(true)
  15.         {
  16.             try {
  17.                 Thread.sleep(5000);
  18.             } catch (Exception e) {}
  19.             smokingtable.setAgentElements();
  20.             // this triggers the smoker-threads to look at the table
  21.             output("The Agent puts " + smokingtable.getAgentElements() + table.");
  22.             // pause the agent while one smoker thread is running
  23.         }
  24.     }
  25.  
  26.  
  27.     public synchronized void wake()
  28.     {
  29.         try
  30.         {
  31.             notify();
  32.         } catch(Exception e){}
  33.     }
  34.  
  35.  
  36.     public synchronized void pause()
  37.     {
  38.         try
  39.         {
  40.             this.wait();
  41.         } catch (Exception e) {}
  42.     }
  43.  
  44.     private void output(String pOutput)
  45.     {
  46.         System.out.println(pOutput);
  47.     }
  48. }
  49.        
  50. public class agent extends Thread {
  51.  
  52.     private table smokingtable;
  53.  
  54.     public agent(table pSmokingtable)
  55.     {
  56.         smokingtable = pSmokingtable;
  57.     }
  58.  
  59.     @Override
  60.     public void run()
  61.     {
  62.         while(true)
  63.         {
  64.             try {
  65.                 Thread.sleep(5000);
  66.             } catch (Exception e) {}
  67.             smokingtable.setAgent1Elements();
  68.  
  69.             output("The Agent 1 puts " + smokingtable.getAgent1Elements());
  70.  
  71.             smokingtable.setAgent2Elements();
  72.             output("The Agent 2 puts " + smokingtable.getAgent2Elements());
  73.  
  74.             smokingtable.setAgent3Elements();
  75.             output("The Agent 3 puts " + smokingtable.getAgent3Elements());
  76.             pause();
  77.         }
  78.     }
  79.  
  80.  
  81.     public synchronized void wake()
  82.     {
  83.         try
  84.         {
  85.             notify();
  86.         } catch(Exception e){}
  87.     }
  88.  
  89.  
  90.     public synchronized void pause()
  91.     {
  92.         try
  93.         {
  94.             this.wait();
  95.         } catch (Exception e) {}
  96.     }
  97.  
  98.     private void output(String pOutput)
  99.     {
  100.         System.out.println(pOutput);
  101.     }
  102. }
  103.        
  104. Thread agent1 = new agent( );
  105. Thread agent2 = new agent( );
  106. Thread agent3 = new agent( );
  107.  
  108. agent1.start( );
  109. agent2.start( );
  110. agent3.start( );
  111.  
  112. agent1.join( );
  113. agent2.join( );
  114. agent3.join( );
  115.        
  116. ExecutorService pool = Executors.newFixedThreadPool( 3 );
  117.  
  118. for ( int i = 0; i < 3; ++i )
  119. {
  120.     pool.execute( new agent( ) );
  121. }
  122.  
  123. // This will wait for your agents to execute
  124. pool.shutdown( );
  125.        
  126. public static void main(String[] args) {
  127. for(int i = 0; i < 3; i++)
  128.   new agent(i).start();
  129. }