Facebook
From s14801, 6 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 243
  1. package zad1;
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import java.awt.GridBagLayout;
  7. import javax.swing.JPanel;
  8. import java.awt.GridBagConstraints;
  9. import java.awt.Insets;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.util.List;
  13. import java.util.Random;
  14. import java.util.concurrent.Callable;
  15. import java.util.concurrent.ExecutorService;
  16. import java.util.concurrent.Executors;
  17. import java.util.concurrent.Future;
  18. import java.util.concurrent.RejectedExecutionException;
  19. import java.util.concurrent.locks.Lock;
  20.  
  21. import javax.swing.JButton;
  22. import java.awt.TextArea;
  23. //dziala przycisk thread trzeba dodac funkcjonalnosci
  24. //dla pozostalych przyciskow.
  25. public class Thread_app {
  26.         int btn1=0,btn2=0,btn3=0,btn4=0;
  27.         private JFrame frame;
  28.         TextArea ta;
  29.         /**
  30.          * Launch the application.
  31.          */
  32.         public static void main(String[] args) {
  33.                 EventQueue.invokeLater(new Runnable() {
  34.                         public void run() {
  35.                                 try {
  36.                                         Thread_app window = new Thread_app();
  37.                                         window.frame.setVisible(true);
  38.                                 } catch (Exception e) {
  39.                                         e.printStackTrace();
  40.                                 }
  41.                         }
  42.                 });
  43.         }
  44.  
  45.         /**
  46.          * Create the application.
  47.          */
  48.         public Thread_app() {
  49.                 initialize();
  50.         }
  51.  
  52.         /**
  53.          * Initialize the contents of the frame.
  54.          */
  55.         //dodac do action listenerow co ma sie stac po kliknieciu
  56.         private void initialize() {
  57.                 frame = new JFrame();
  58.                 frame.setBounds(100, 100, 450, 300);
  59.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60.                 GridBagLayout gridBagLayout = new GridBagLayout();
  61.                 gridBagLayout.columnWidths = new int[]{0, 0};
  62.                 gridBagLayout.rowHeights = new int[]{0, 196, 0, 0};
  63.                 gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
  64.                 gridBagLayout.rowWeights = new double[]{0.0, 1.0, 1.0, Double.MIN_VALUE};
  65.                 frame.getContentPane().setLayout(gridBagLayout);
  66.                
  67.                 JButton stop_btn = new JButton("STOP");
  68.                 GridBagConstraints gbc_stop_btn = new GridBagConstraints();
  69.                 gbc_stop_btn.insets = new Insets(0, 0, 5, 0);
  70.                 gbc_stop_btn.gridx = 0;
  71.                 gbc_stop_btn.gridy = 0;
  72.                 frame.getContentPane().add(stop_btn, gbc_stop_btn);
  73.                
  74.                 ta = new TextArea();
  75.                 GridBagConstraints gbc_textArea = new GridBagConstraints();
  76.                 gbc_textArea.insets = new Insets(0, 0, 5, 0);
  77.                 gbc_textArea.gridx = 0;
  78.                 gbc_textArea.gridy = 1;
  79.                 frame.getContentPane().add(ta, gbc_textArea);
  80.                
  81.                 JPanel panel = new JPanel();
  82.                 GridBagConstraints gbc_panel = new GridBagConstraints();
  83.                 gbc_panel.fill = GridBagConstraints.BOTH;
  84.                 gbc_panel.gridx = 0;
  85.                 gbc_panel.gridy = 2;
  86.                 frame.getContentPane().add(panel, gbc_panel);
  87.                
  88.                 JButton thread1_btn = new JButton(" Thread 1");
  89.                 panel.add(thread1_btn);
  90.                
  91.                 JButton thread2_btn = new JButton("Thread 2");
  92.                 panel.add(thread2_btn);
  93.                
  94.                 JButton thread3_btn = new JButton("Thread 3");
  95.                 panel.add(thread3_btn);
  96.                
  97.                 JButton thread4_btn = new JButton("Thread 4");
  98.                 panel.add(thread4_btn);
  99.                  
  100.                 //action listenery
  101.                 thread1_btn.addActionListener(new ActionListener(){//startowanie theardow
  102.                          
  103.                         @Override
  104.                         public void actionPerformed(ActionEvent arg0) {
  105.                                 // TODO Auto-generated method stub
  106.                                 if(btn1==0){
  107.                                         taskStart();
  108.                                         thread1_btn.setText("Suspend");
  109.                                 }
  110.                                 if(btn1==1){
  111.                                         thread1_btn.setText("Continue");
  112.                                 }
  113.                                 if(btn1==2){
  114.                                         thread1_btn.setText("Suspend");
  115.                                         btn1=0;
  116.                                 }
  117.                                 btn1++;
  118.                         }
  119.                 });
  120.                
  121.                 thread2_btn.addActionListener(new ActionListener(){
  122.  
  123.                         @Override
  124.                         public void actionPerformed(ActionEvent e) {
  125.                                 // TODO Auto-generated method stub
  126.                                 if(btn2==0){
  127.                                         taskStart();
  128.                                         thread2_btn.setText("Suspend");
  129.                                 }
  130.                                 if(btn2==1){
  131.                                         thread2_btn.setText("Continue");
  132.                                 }
  133.                                 if(btn2==2){
  134.                                         thread2_btn.setText("Suspend");
  135.                                         btn2=0;
  136.                                 }
  137.                                 btn2++;
  138.                         }
  139.                 });
  140.                
  141.                 thread3_btn.addActionListener(new ActionListener(){
  142.  
  143.                         @Override
  144.                         public void actionPerformed(ActionEvent e) {
  145.                                 // TODO Auto-generated method stub
  146.                                 if(btn3==0){
  147.                                         taskStart();
  148.                                         thread3_btn.setText("Suspend");
  149.                                 }
  150.                                 if(btn3==1){
  151.                                         thread3_btn.setText("Continue");
  152.                                 }
  153.                                 if(btn3==2){
  154.                                         thread3_btn.setText("Suspend");
  155.                                         btn3=0;
  156.                                 }
  157.                                 btn3++;
  158.                         }
  159.                 });
  160.                
  161.                 thread4_btn.addActionListener(new ActionListener(){
  162.  
  163.                         @Override
  164.                         public void actionPerformed(ActionEvent e) {
  165.                                 // TODO Auto-generated method stub
  166.                                 if(btn4==0){
  167.                                         taskStart();
  168.                                         thread4_btn.setText("Suspend");
  169.                                 }
  170.                                 if(btn4==1){
  171.                                         thread4_btn.setText("Continue");
  172.                                 }
  173.                                 if(btn4==2){
  174.                                         thread4_btn.setText("Suspend");
  175.                                         btn4=0;
  176.                                 }
  177.                                 btn4++;
  178.                         }
  179.                 });
  180.                
  181.                 stop_btn.addActionListener(new ActionListener(){
  182.  
  183.                         @Override
  184.                         public void actionPerformed(ActionEvent e) {// zatrzymanie dzialania watkow.
  185.                                 // TODO Auto-generated method stub
  186.                                 thread1_btn.setText("Continue");
  187.                                 thread3_btn.setText("Continue");
  188.                                 thread4_btn.setText("Continue");
  189.                                 thread2_btn.setText("Continue");
  190.                         }                      
  191.                 });
  192.         }
  193.        
  194.         class SumTask implements Callable<Integer> {
  195.  
  196.             private int taskNum,
  197.                         limit,
  198.                                 value;
  199.             int sum = 0;
  200.             public SumTask(int taskNum, int limit) {
  201.               this.taskNum = taskNum;
  202.               this.limit = limit*taskNum;
  203.               //losowa wartosc z podanego przedzialu
  204.             }
  205.  
  206.             public Integer call() throws Exception {
  207.               Random generator = new Random();
  208.              
  209.               while (sum <= limit) {
  210.                 value=generator.nextInt(100*taskNum)+1;
  211.                 if (Thread.currentThread().isInterrupted()) return null;
  212.                 sum=sum+value;
  213.                 ta.append("Thread " + taskNum + " (limit="+limit+"): "+value+", sum="+ sum + '\n');
  214.                 Thread.sleep(1000);//zmienic na wartosc losowa
  215.               }
  216.               //zakonczenie threada
  217.                 ta.append("Thread " + taskNum +" is done."+'\n');
  218.  
  219.               return sum;
  220.             }
  221.           };
  222.  
  223.           int k = 0;
  224.           int n = 1000;
  225.           Future<Integer> task;
  226.           ExecutorService exec = Executors.newFixedThreadPool(4);
  227.         public void taskStart() {
  228.             try {
  229.               task = exec.submit(new SumTask(++k, n));
  230.             } catch(RejectedExecutionException exc) {
  231.                 ta.append("Execution rejected\n");
  232.                 return;
  233.             }
  234.             ta.append("Thread " + k + " submitted\n");
  235.           }
  236.           public void taskShutdown() {
  237.             exec.shutdown();
  238.           }
  239.          public boolean taskDone(){
  240.                  return task.isDone();
  241.          }
  242.           public void taskShutdownNow() {
  243.             List<Runnable> awaiting = exec.shutdownNow();
  244.             ta.append("Executor shutdown now - awaiting tasks:\n");
  245.             for (Runnable r : awaiting) {
  246.               ta.append(r.getClass().getName()+'\n');
  247.             }
  248.  
  249.          }
  250. }
  251.