Illustrates multithread UI programming issues : Thread « SWT JFace Eclipse « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » SWT JFace Eclipse » Thread 




Illustrates multithread UI programming issues
Illustrates multithread UI programming issues

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 
 * Created on Oct 25, 2003 4:12:32 PM by JACK
 * $Id: PICalculator.java,v 1.1 2003/12/22 12:07:54 jackwind Exp $
 
 * visit: http://www.asprise.com/swt
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Illustrates multithread UI programming issues.
 */
public class PICalculator {
  Display display = new Display();
  Shell shell = new Shell(display);
  Button buttonThread = new Button(shell, SWT.PUSH);
  Button buttonAsyncExec = new Button(shell, SWT.PUSH);

  public PICalculator(boolean asyncExecEnabled) {
    final boolean async = asyncExecEnabled;

    shell.setText("PI Calculator");
    shell.setSize(40080);

    Rectangle clientArea = shell.getClientArea();

    buttonThread.setText(
      "Click here to calculate PI  [Non-UI thread UI Update]");
    buttonThread.setBounds(
      clientArea.x,
      clientArea.y,
      clientArea.width,
      clientArea.height / 2);
    buttonThread.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent e) {
      }
      public void widgetSelected(SelectionEvent e) {
        buttonThread.setText("Calculation in progress ...");
        getTask(buttonThread).start();
      }
    });
    
    buttonAsyncExec.setText("Click here to calculate PI  [asynExec method UI Update]");
    buttonAsyncExec.setBounds(
      clientArea.x,
      clientArea.y + clientArea.height / 2,
      clientArea.width,
      clientArea.height / 2);
    buttonAsyncExec.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent e) {
      }
      public void widgetSelected(SelectionEvent e) {
        buttonAsyncExec.setText("Calculation in progress ...");
        getTask2(buttonAsyncExec).start();
      }
    });
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }

    display.dispose();
  }

  public static void main(String[] args) {
    // new CalculatePI(false);
    new PICalculator(true);
  }

  public Thread getTask(Button button) {
    final Button theButton = button;
    return new Thread() {
      public void run() {
        double pi = calculatePI(9999999);
        theButton.setText("PI = " + pi)// Update UI.
      }
    };
  }

  public Thread getTask2(Button button) {
    final Button theButton = button;
    return new Thread() {

      public void run() {
        final double pi = calculatePI(9999999);
        
        display.asyncExec(new Runnable() {
          public void run() {
            
            // Update UI.
            
            theButton.setText("PI = " + pi)
            
          }
        });
      }
    };
  }  
  /**
   * Calculate value of PI using Vieta's formula. For a complete discussion,
   * please visit:
   * http://documents.wolfram.com/v4/GettingStarted/CalculatingPi.html
   
   @param nestedLevel -
   *            level of nested square roots in Vieta's formula.
   @return value of PI
   */
  public static double calculatePI(int nestedLevel) {
    double product = 1;
    double lastSqrtValue = 0;

    for (int i = 0; i < nestedLevel; i++) {
      double sqrt = getNextSqrtValue(lastSqrtValue);
      product *= 0.5 * sqrt;

      lastSqrtValue = sqrt;
    }

    return / product;
  }

  /**
   * Return the square root item value.
   
   @param lastValue -
   *            last square root item value.
   @return
   */
  public static double getNextSqrtValue(double lastValue) {
    return Math.sqrt(+ lastValue);
  }
}



           
       














Related examples in the same category
1.SWT and ThreadSWT and Thread
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.