2

After running my SWT application it doesn’t have any error at the time of compilation. But after running it will shows the output for few sec and then the eclipse instance will become not responsive. Please help me to avoid exceptions I try to increase heap size.anybody is here to help me.....plzzzzzzzzzzzzzzzzzzzz

here i will give my code. i think my logic also have a problem. but i dont know how to correct it. i just follow on book to do this.here it is a program for clock. and i did in each movement of second hand a new thread is created. i want to make it as one thread. it shows unable to create new native threads activator.java

package com.packtpub.e4.clock.ui;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
    * The activator class controls the plug-in life cycle
*/
    public class Activator extends AbstractUIPlugin {

// The plug-in ID
public static final String PLUGIN_ID = "com.packtpub.e4.clock.ui"; //$NON-NLS-1$

// The shared instance
private static Activator plugin;
private TrayItem trayItem;
private Image image;
private Shell shell;



/**
 * The constructor
 */
public Activator() {
}

/*
 * (non-Javadoc)
 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
 */
public void start(BundleContext context) throws Exception {
    super.start(context);
    plugin = this;
    final Display display = Display.getDefault();
    display.asyncExec(new Runnable() {
    public void run() {

        image = new Image(display, Activator.class.getResourceAsStream("/icons/sample.gif"));
                Tray tray = display.getSystemTray();
                if (tray != null && image != null) {
                trayItem = new TrayItem(tray, SWT.NONE);
                trayItem.setToolTipText("Hello World");
                trayItem.setVisible(true);
                trayItem.setText("Hello World");
                trayItem.setImage(new Image(trayItem.getDisplay(),
                Activator.class.getResourceAsStream("/icons/sample.gif")));
                }
                trayItem.addSelectionListener(new SelectionListener() {
                    public void widgetSelected(SelectionEvent e) {
                    if (shell == null) {
                    shell = new Shell(trayItem.getDisplay());
                    shell.setLayout(new FillLayout());
                    new ClockWidget(shell, SWT.NONE, new RGB(255, 0, 255));
                    shell.pack();
                    }
                    shell.open();
                    }

                    @Override
                    public void widgetDefaultSelected(SelectionEvent e) {
                        // TODO Auto-generated method stub

                    }
                });

                }
                });




}

/*
 * (non-Javadoc)
 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
 */
public void stop(BundleContext context) throws Exception {

        if (trayItem != null && !trayItem.isDisposed()) {
        Display.getDefault().asyncExec(new Runnable() {
        public void run() {
        if (trayItem != null && !trayItem.isDisposed())
            trayItem.dispose();
        }
        });
        }
        if (image != null && !image.isDisposed()) {
        Display.getDefault().asyncExec(new Runnable() {
        public void run() {
        if (image != null && !image.isDisposed())
        image.dispose();
        }
        });
        }
        if (shell != null && !shell.isDisposed()) {
            Display.getDefault().asyncExec(new Runnable() {
            public void run() {
            if (shell != null && !shell.isDisposed())
            shell.dispose();
            }
            });
            }



}

/**
 * Returns the shared instance
 *
 * @return the shared instance
 */
public static Activator getDefault() {
    return plugin;
}

/**
 * Returns an image descriptor for the image file at the given
 * plug-in relative path
 *
 * @param path the path
 * @return the image descriptor
 */
public static ImageDescriptor getImageDescriptor(String path) {
    return imageDescriptorFromPlugin(PLUGIN_ID, path);
}

}

clockwidget.java

package com.packtpub.e4.clock.ui;

import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

public class ClockWidget extends Canvas 
{
private final Color color;
private int offset;
public void setOffset(int offset)
{
this.offset = offset;
}


public Color getColor()
{
    return color;
}


public int getOffset() 
{
    return offset;
}


public ClockWidget(Composite parent, int style,RGB rgb) 
{
    super(parent, style);
    this.color = new Color(parent.getDisplay(),rgb);
    addDisposeListener(new DisposeListener() 
    {
        public void widgetDisposed(DisposeEvent e)
        {
            if(color != null && !color.isDisposed())
                color.dispose();
        }
    });

    addPaintListener(new PaintListener()
    {
        public void paintControl(PaintEvent e)
        {
            ClockWidget.this.paintControl(e);
        }
    });

}

public void paintControl(PaintEvent e)
{
    @SuppressWarnings("deprecation")
    int seconds = new Date().getSeconds();
    int arc = (15-seconds) * 6 % 360;
    e.gc.setBackground(color);
    e.gc.fillArc(e.x,e.y,e.width-1,e.height-1,arc-1,2);
    e.gc.drawArc(e.x,e.y,e.width-1,e.height-1,0,360);
    e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK));
    @SuppressWarnings("deprecation")
    int hours = new Date().getHours() + offset;
    arc = (3 - hours) * 30 % 360;
    e.gc.fillArc(e.x, e.y, e.width-1, e.height-1, arc - 5, 10);

    new Thread("TickTock")
    {
        public void run()
        {
            while (!ClockWidget.this.isDisposed()) 
            {

                ClockWidget.this.getDisplay().asyncExec(
                new Runnable()
                {
                    public void run() 
                    {
                        if (!ClockWidget.this.isDisposed())
                            ClockWidget.this.redraw();

                    }
                });
                try
                {
                    Thread.sleep(99999);
                } 
                catch (InterruptedException e)
                {
                    System.out.println("@clock"+e.toString());
                    return;
                }
            }
        }
    }.start();



}


public Point computeSize(int w,int h,boolean changed)
{
    int size;
    if(w == SWT.DEFAULT) 
    {
        size = h;
    } 
    else if (h == SWT.DEFAULT)
    {
        size = w;
    } 
    else
    {
        size = Math.min(w,h);
    }
    if(size == SWT.DEFAULT)
        size = 50;
    return new Point(size,size);
}

}

SampleView.java

package com.packtpub.e4.clock.ui.views;


import java.util.TimeZone;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.*;
import org.eclipse.swt.SWT;

import com.packtpub.e4.clock.ui.ClockWidget;




public class SampleView extends ViewPart {

private Combo timezones;



public void createPartControl(Composite parent) {
    try{
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    parent.setLayout(layout);
    Object[] oo=parent.getDisplay().getDeviceData().objects;
    int c = 0;
    for (int j = 0; j < oo.length; j++)
    if (oo[j] instanceof Color)
    c++;
    System.err.println("There are " + c + " Color instances");

    final ClockWidget clock1 =new ClockWidget(parent, SWT.NONE, new RGB(255,0,0));
    //final ClockWidget clock2 =new ClockWidget(parent, SWT.NONE, new RGB(0,255,0));
    //final ClockWidget clock3 =new ClockWidget(parent, SWT.NONE, new RGB(0,0,255));

    //clock1.setLayoutData(new RowData(20,20));
    //clock3.setLayoutData(new RowData(100,100));
    String[] ids = TimeZone.getAvailableIDs();
    timezones = new Combo(parent, SWT.SIMPLE);
    timezones.setVisibleItemCount(5);
    for (int i = 0; i < ids.length; i++) {
    timezones.add(ids[i]);

    timezones.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
        String z = timezones.getText();
        TimeZone tz = z == null ? null : TimeZone.getTimeZone(z);
        TimeZone dt = TimeZone.getDefault();
        int offset = tz == null ? 0 : (
                tz.getOffset(System.currentTimeMillis()) -
                dt.getOffset(System.currentTimeMillis())) / 3600000;
                clock1.setOffset(offset);
                clock1.redraw();
                }
                public void widgetDefaultSelected(SelectionEvent e) {
                clock1.setOffset(0);
                clock1.redraw();
                }
                });





    }

}catch(Exception e){
    System.out.println("@ SampleView.java"+e.toString());
}

}

    public void setFocus() {
        timezones.setFocus();

    }
    }
Durga
  • 319
  • 1
  • 6
  • 18
  • Did you read [this](http://stackoverflow.com/questions/6754628/eclipse-crashes-with-unable-to-create-new-native-thread-any-ideas-my-setti)? – Baz Jan 22 '14 at 09:58
  • If you're running out of memory, I'd imagine you have a leak somewhere. Bear in mind that compiled code can have a different memory footprint compared to uncompiled code ran from within eclipse. – Gorbles Jan 22 '14 at 10:17
  • i already write code for memory leak.. after dat also same problem – Durga Jan 22 '14 at 10:23
  • public void createPartControl(Composite parent) { Object[] oo=parent.getDisplay().getDeviceData().objects; int c = 0; for (int i = 0; i < oo.length; i++) if (oo[i] instanceof Color) c++; System.err.println("There are " + c + " Color instances"); public ClockWidget(Composite parent, int style, RGB rgb) { super(parent, style); this.color = new Color(parent.getDisplay(),rgb); addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { if(color != null && !color.isDisposed()) color.dispose(); } }); } – Durga Jan 22 '14 at 10:24
  • @Baz : i already tried increasing the heap size.. – Durga Jan 22 '14 at 10:30
  • @PathuZ What about stack size? – Baz Jan 22 '14 at 10:38
  • no i didnt try... actualy am new to all these... – Durga Jan 22 '14 at 10:43
  • @Baz hw to increase stack size.. wil u please help me...:( – Durga Jan 22 '14 at 10:48
  • @PathuZ Read [this](http://crunchify.com/jvm-tuning-heapsize-stacksize-garbage-collection-fundamental/) – Baz Jan 22 '14 at 10:52
  • @Baz i tried ... stil it shows unhandled event loop exception – Durga Jan 22 '14 at 11:01
  • 1
    We'll only be able to help you if you post your code, of even better, a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve). – Baz Jan 22 '14 at 11:03
  • 1
    @PathuZ - Don't simple accept any answer just for the sake of it. The currently accepted answer is just a "me too" comment, which the OP posted as they did not have enough rep to post it as a comment. – Rahul Jan 23 '14 at 06:29
  • @R.J from yesterday we are try to solve dis.so for reach of my question i accept that answer. – Durga Jan 23 '14 at 06:39
  • @PathuZ - Actually, if you do that, you reach gets over(kind of). If a question already has an accepted answer, why would people look into it(except for few enthusiastic folks)? Doing this would hamper your chances of getting a proper answer. That is why I recommend you to not do it. – Rahul Jan 23 '14 at 06:59
  • @R.J thanks for ur recomendation.... anyway i got the answer... and we do it ourself.... – Durga Jan 23 '14 at 07:58

1 Answers1

1

i got the answer... here the thread call is happend in clockwidgets paintcontrol function cut it from there and paste it on clockwidget constructor. then the code will work proper package com.packtpub.e4.clock.ui;

import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

public class ClockWidget extends Canvas 
  {
     private final Color color;
     private int offset;
     public void setOffset(int offset)
{
this.offset = offset;
}


public Color getColor()
{
    return color;
}


public int getOffset() 
{
    return offset;
}


public ClockWidget(Composite parent, int style,RGB rgb) 
{
    super(parent, style);
    this.color = new Color(parent.getDisplay(),rgb);


    addDisposeListener(new DisposeListener() 
    {
        public void widgetDisposed(DisposeEvent e)
        {
            if(color != null && !color.isDisposed())
                color.dispose();
        }
    });
    new Thread("TickTock")
    {
        public void run()
        {
            while (!ClockWidget.this.isDisposed()) 
            {

                ClockWidget.this.getDisplay().asyncExec(
                new Runnable()
                {
                    public void run() 
                    {
                        if (!ClockWidget.this.isDisposed())
                            ClockWidget.this.redraw();

                    }
                });
                try
                {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException e)
                {

                    return;
                }
            }
        }
    }.start();

    addPaintListener(new PaintListener()
    {
        public void paintControl(PaintEvent e)
        {
            ClockWidget.this.paintControl(e);


        }
    });

}

public void paintControl(PaintEvent e)
{
    @SuppressWarnings("deprecation")
    int seconds = new Date().getSeconds();
    int arc = (15-seconds) * 6 % 360;
    e.gc.setBackground(color);
    e.gc.fillArc(e.x,e.y,e.width-1,e.height-1,arc-1,2);
    e.gc.drawArc(e.x,e.y,e.width-1,e.height-1,0,360);
    e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK));
    @SuppressWarnings("deprecation")
    int hours = new Date().getHours() + offset;
    arc = (3 - hours) * 30 % 360;
    e.gc.fillArc(e.x, e.y, e.width-1, e.height-1, arc - 5, 10);

}


public Point computeSize(int w,int h,boolean changed)
{
    int size;
    if(w == SWT.DEFAULT) 
    {
        size = h;
    } 
    else if (h == SWT.DEFAULT)
    {
        size = w;
    } 
    else
    {
        size = Math.min(w,h);
    }
    if(size == SWT.DEFAULT)
        size = 50;
    return new Point(size,size);
}

}

Durga
  • 319
  • 1
  • 6
  • 18