473,770 Members | 6,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting data out of a thread... how?

blazedaces
284 Contributor
Alright, so I racked my brain over this problem and thought maybe I could work with it kind of like FileDialog seems to work in that you create a new FileDialog(vari ables needed) and then after it runs (after you setVisible(true )) you can call something to get the information that was stored inside like this: fd.getDirectory ()...

So I tried something like that here:

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String args[]) {
  2.         ArrayList<String> temp = new ArrayList<String>(2);
  3.         temp.add("First Tag\\");
  4.         temp.add("Second Tag\\");
  5.         final TagChoosingWindow TCW = new TagChoosingWindow(temp);
  6.         //Schedule a job for the event-dispatching thread:
  7.         //creating and showing this application's GUI.
  8.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  9.             public void run() {
  10.                 TCW.createAndShowGUI(); 
  11.             }
  12.         });
  13.         Utilities.print(TCW.getAL()); //Should print the arraylist to the screen one component at a time
  14.     }
  15.  
but this spits out a NullPointerExce ption error because main obviously doesn't wait for the thread to finish before trying to access TCW.getAL()...

So how do I get around this? This GUI is supposed to finish when you press the done button and I hope I can then just call a method .getAL() and get my ArrayList when it's done.

What do you guys suggest? I've had zero experience with threading before so this simple concept of getting information out of a thread is eluding me. I want to avoid having the thread do some large operation when it's finished.


Thanks for all your help
-blazed
Jul 24 '07 #1
7 2139
JosAH
11,448 Recognized Expert MVP
Alright, so I racked my brain over this problem and thought maybe I could work with it kind of like FileDialog seems to work in that you create a new FileDialog(vari ables needed) and then after it runs (after you setVisible(true )) you can call something to get the information that was stored inside like this: fd.getDirectory ()...

So I tried something like that here:

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String args[]) {
  2.         ArrayList<String> temp = new ArrayList<String>(2);
  3.         temp.add("First Tag\\");
  4.         temp.add("Second Tag\\");
  5.         final TagChoosingWindow TCW = new TagChoosingWindow(temp);
  6.         //Schedule a job for the event-dispatching thread:
  7.         //creating and showing this application's GUI.
  8.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  9.             public void run() {
  10.                 TCW.createAndShowGUI(); 
  11.             }
  12.         });
  13.         Utilities.print(TCW.getAL()); //Should print the arraylist to the screen one component at a time
  14.     }
  15.  
but this spits out a NullPointerExce ption error because main obviously doesn't wait for the thread to finish before trying to access TCW.getAL()...

So how do I get around this? This GUI is supposed to finish when you press the done button and I hope I can then just call a method .getAL() and get my ArrayList when it's done.

What do you guys suggest? I've had zero experience with threading before so this simple concept of getting information out of a thread is eluding me. I want to avoid having the thread do some large operation when it's finished.


Thanks for all your help
-blazed
Your (main) thread can 'join' the other thread: when it stops, your thread continues
and you can grab the ArrayList from the other object.

kind regards,

Jos
Jul 24 '07 #2
blazedaces
284 Contributor
Your (main) thread can 'join' the other thread: when it stops, your thread continues
and you can grab the ArrayList from the other object.

kind regards,

Jos
Unfortunately I'm still making TCW final which means it can not change right? Yet I can't access a non-final variable inside the run() area... ? I'll try using join() ... thank you.

-blazed
Jul 24 '07 #3
blazedaces
284 Contributor
Alright, so my program has been modifed, I now have a class which extends thread that is as follows (very simple):

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class runTCW extends Thread { 
  4.     TagChoosingWindow TCW;
  5.     public runTCW(ArrayList<String> al) {
  6.         TCW = new TagChoosingWindow(al);
  7.     }
  8.  
  9.     public void run() {
  10.         TCW.createAndShowGUI();
  11.     }
  12.  
  13.     public TagChoosingWindow getTCW() { return this.TCW; }
  14. }
  15.  
And my main thread now looks like this:

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String args[]) {
  2.         ArrayList<String> temp = new ArrayList<String>(2);
  3.         temp.add("First Tag\\");
  4.         temp.add("Second Tag\\");
  5.  
  6.         runTCW rTCW = new runTCW(temp);
  7.         rTCW.start();
  8.         try {
  9.             rTCW.join();
  10.         } catch (InterruptedException e) {
  11.             e.printStackTrace();
  12.         }
  13.  
  14.         Utilities.print(rTCW.getTCW().getNames());
  15.     }
  16.  
It's STILL throwing a nullPointerExce ption on that line that says "Utilities.prin t(rTCW.getTCW() .getNames());"

I'm wondering why this could be... after all it makes sense that the thread has finished. It could be a certain line of code where I do frame.dispose()... but I thought that the variables not associated with the GUI would disappear, but not the arraylist I may have had stored somewhere.

Again thanks for the help,

-blazed

P.S.

The nullPointerExce ption is thrown before the thread finishes so that brings me to the conclusion that I'm closing the thread incorrectly or join() is not working! Excuse my lack of experience, I'm sure I'm doing something wrong...

I'm trying to look up examples of others using join() but it's not going well...
Jul 24 '07 #4
blazedaces
284 Contributor
I tried to add a .isAlive() printed afterwards to see if the join() is working... well, obviously it is:

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String args[]) {
  2.         ArrayList<String> temp = new ArrayList<String>(2);
  3.         temp.add("First Tag\\");
  4.         temp.add("Second Tag\\");
  5.  
  6.         runTCW rTCW = new runTCW(temp);
  7.         rTCW.start();
  8.         try {
  9.             rTCW.join();
  10.         } catch (InterruptedException e) {
  11.             e.printStackTrace();
  12.         }
  13.  
  14.         System.out.println(rTCW.isAlive());
  15.         Utilities.print(rTCW.getTCW().getNames());
  16.     }
  17.  
Prints the following:

Expand|Select|Wrap|Line Numbers
  1. false
  2. Exception in thread "main" java.lang.NullPointerException
  3.     at AsafUtil.Utilities.print(Utilities.java:139)
  4.     at TagChoosingWindow.main(TagChoosingWindow.java:116)
  5.  
  6. Process completed.
  7.  
So clearly I'm doing something else wrong... It must have to do with my createAndShowGu i method right? Is there some way to cause the thread to keep running until the frame is closed?

Thanks for your help again,

-blazed
Jul 24 '07 #5
blazedaces
284 Contributor
Alright, so I basically fixed my problem, but if someone who knows what they're doing reads this maybe you can tell me if there's a more conventional solution out there. This is what my createAndShowGU I() method now looks like:

Expand|Select|Wrap|Line Numbers
  1. public void createAndShowGUI() {
  2.         //Create and set up the window.
  3.         frame = new JFrame("TableDemo");
  4.         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //Don't ask me why a space appears between the C and LOSE
  5.         frame.setContentPane(this);
  6.  
  7.         //Display the window.
  8.         frame.pack();
  9.         frame.setVisible(true);
  10.  
  11.         while(frame.isVisible()) { } //THIS IS THE LINE I ADDED
  12.     }
  13.  
Jul 24 '07 #6
JosAH
11,448 Recognized Expert MVP
Shouldn't you use a modal JDialog instead? It takes care of suspending
other threads.

kind regards,

Jos
Jul 25 '07 #7
blazedaces
284 Contributor
Shouldn't you use a modal JDialog instead? It takes care of suspending
other threads.

kind regards,

Jos
This is a much better solution... I have to finish this asap, but I will come back and make it a JDialog later...

Thank you for the help,

-blazed
Jul 25 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
4238
by: Vinod I | last post by:
Hi Team, I am having a string as "System.Data.SqlDbType.Int". Now I want to convert this string type to actual type to use with my Command object Parameter Creation. How I will convert this string to the type object ?. Thanks in advance
9
3268
by: Microsoft News Server | last post by:
Hi, I am currently having a problem with random, intermittent lock ups in my ASP.net application on our production server (99% CPU usage by 3 threads, indefinately). I currently use IIS Debug Tools to do a memory dump of the app when the lock up occurs, however the stack information is not very useful. I have just put a new build of our system onto production, and this build is a "Debug" build as opposed to a "Release" build. I am...
3
3879
by: Michael Glass | last post by:
I'm working on an ASP.Net web app using VS2005 and the .Net 2.0 framework, and I have a serious problem with the page I'm currently working on. The page has, among other things, two FormViews and a GridView control, each with its own SqlDataSource. FormView1 talks to my Opportunity table and has an ItemTemplate and an EditItemTemplate. FormView2 talks to my Activities table and has an ItemTemplate, InsertItemTemplate and an...
14
4452
by: Ankit Aneja | last post by:
The code of classes given below is for server to which clients connect i want to get ip address of client which has connected pls help how can i get //listen class public class listen {
1
2525
by: Fred B | last post by:
I am launching a new thread from my application's main process (using VB.net 2003), and I can't get the child to receive the parameter I'm attempting to send it in a named data slot. The code for launching the thread: Dim NewThread As New Thread(AddressOf LaunchCommThread) NewThread.AllocateNamedDataSlot("Offset") NewThread.IsBackground = True NewThread.Name = SIMclass.SIM(1).strCtrlDesignator
5
2133
by: Angus | last post by:
Hello I am doing this in a worker thread: char* szPartial = new char; if (szPartial) { lstrcpy(szPartial, szBuffer); PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial); }
33
11865
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon (NT_AUTHORITY\SYSTEM). I understand that when the service starts, no user may be logged in, but that's ok, as the app I am monitoring can only be run by a logged in user. Do I need to use WMI to get the user context of Explorer.exe or is there a...
1
2667
by: Tenowg | last post by:
Hey everyone... Firstly let me tell you this is a project that is probably alittle over my head... I have played with programming most of my life, but I have only been doing VB 2005 for about 2 weeks. I know the concepts of most issues, but multi-threading seems to be giving me a headache... anyways here is what is happening... I have use the MSDN code sample to create a Async TCP server (using sockets, and beginaccept, etc, not Tcplistener...
3
3317
by: Sami Vaisanen | last post by:
Hello group, I'm writing a C++ based application that embeds the python engine. Now I have a problem regarding exception/error information. Is there a way to get the exception message and possibly the traceback into a string for example? I've been eyeballing the PyErr_ module and it seems fairly limited. In other words PyErr_Print() calls the right functions for getting the exception information but unfortunately it is hardwired to...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.