Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Threading not functioning

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Threading not functioning

    Hello,

    I'm currently working on a project that executes queries to the database. This process takes a long time to be finished, so I would like the user to be able to use the JFrame while its processing. The solution I found is using Threads.

    I never used it before and after some tutorials online I found a way to use it. The problem is, the JFrame is still freezing even when the query is executed within a thread. (I think)

    Because the project is private I can't post the code here. I did however create an example code to make my (maybe mistake or problem) clear. The project has the follwing structure:

    [Package]
    [--> class] Main
    [--> class] Form (extends JFrame)
    [--> class] Timer (implements Runnable)

    The way I coded this is as follows:

    Main class:
    package Package;
     
    import java.awt.Color;
     
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
     
    public class Main {
     
    	public static void main(String[] args) 
    	{
    		Form form = new Form();	
    		form.setVisible(true);
            	form.setSize(288, 435);
            	form.setResizable(false);
            	form.setLayout(null);
            	form.getContentPane().setBackground(Color.black);
            	form.setIconImage(new ImageIcon(form.getClass().getResource("http://www.java-forums.org/images/Marque-it.ico")).getImage());
            	form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }

    Form class:
    package Package;
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.swing.*;
     
    public class Form extends JFrame implements MouseListener  {
     
    	private static final long serialVersionUID = 1L;
     
    	private Thread[] treadList;
     
    	public Form ()
    	{
    		// Use a null layout
    		setLayout(null);
     
    		// Load buttons and images ect
    		loadDesign();
     
    		// Create new threads
    		for(int i = 0; i < treadList.length; i++)
    		{
     
    			treadList[i] = new treadList(new Timer(5000, this));
    			treadList[i].start();
    		}
     
             }
     
    }

    Timer class:
    package Package;
     
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.MalformedURLException;
    import java.net.URL;
     
     
    //public class Timer implements Runnable {
    public class Timer implements Runnable
    {
    	 Toolkit toolkit;
    	 int seconds;
    	 private Form frm;
     
    	public Timer(int seconds, Form frm)
    	{
    	    toolkit = Toolkit.getDefaultToolkit();
     
    	    this.seconds = seconds;
     
    	    this.frm = frm;
     
    	}
     
      javax.swing.Timer timer = new javax.swing.Timer(seconds, new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	// Do query what takes long time
            }
         });
     
     
     
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		System.out.println(Thread.currentThread());
    		timer.start();
    	}
     
    }


    I don't know what is wrong with this code? Although I need to say I don't have much experience with programming in Java. I also tried to use a SwingWorker but it's the same result.


    Can someone help me?

    Kind regards,

    Mike Feonx


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Threading not functioning

    This thread has been cross posted here:

    http://www.java-forums.org/awt-swing/50471-threading-not-functioning.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. Replies: 3
    Last Post: October 19th, 2011, 07:57 AM
  2. Connection pool and Datasource Implementation Is Not Functioning
    By mychickbad in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 14th, 2011, 06:15 PM
  3. Possible threading issue
    By Kerr in forum Threads
    Replies: 3
    Last Post: March 6th, 2011, 05:24 PM
  4. problem in Threading !!!
    By roadies07 in forum Threads
    Replies: 4
    Last Post: July 14th, 2010, 10:21 AM
  5. Threading question....
    By neo_2010 in forum Threads
    Replies: 1
    Last Post: September 2nd, 2009, 02:38 AM