Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Java Loops

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I am experimenting with Java loops. I am attempting to create a splash screen using a while loop. The idea is that whilst the program/application (not sure what to call it, the code that I've written) is being loaded, a splash screen appears. However the code that I have written is not acting as I expected...

I've posted the code below in case my explanation doesn't make sense, or is incorrect :S

I am using two threads. The first thread (named MainThread) is started, with the intention that everything will be loaded with this thread. At the beginning of the first thread (MainThread), a second thread is created (named SplashThread). The second thread (SplashThread) is what is supposed to create and maintain the splash screen.

My expectation was that the code



would continue to run until the first thread (MainThread) called the second thread (SplashThread) to change the boolean (named AppLoading). In the code (which is below) I have put comments at the start and end of all methods. What seems to be happening using these comments is that the MainThread is starting, running through all of the code expected, and then finishing. However, the SplashThread appears to be running through the code, and then simply finishing before the MainThread has changed the boolean that is due to finish the SplashThread while loop.

Gosh that sounds complicated...

I am aware that Java comes with a SplashScreen feature built in, however my understanding is that this will be a separate screen altogether. What I am aiming to do is create a JFrame, show a loading page within the JFrame, and then, once everything is read and loaded, show my main menu.

In short I have three questions.

1. If a while loop has nothing do to, will it just exit and continue as if there was no while loop (thats what appears to be happening)?

2. am I misunderstanding what the Splash Screen feature in Java does? Is it possible to have a splash screen inside a JFrame?

I have been using this page from the  Java Documentation to try and understand more about Splash Screen in Java. Have I misunderstood it?

3. I would normally refer to what I am attempting to create as a Loading Screen, however upon googling all I can find is a Splash Screen. I may have the incorrect understanding of the difference between the two (if any), I have always known a loading screen as a screen that is inside the program, usually shown between parts of the program itself, for example, press a button, program shows loading screen, program shows expected screen, whereas I have always known a splash screen as a little image that appears immediately after starting a program... Am I misunderstood here too?

Code for both threads is below if needed.

Thank you for reading, and any help you can provide.

Jack

EDIT 1***

I've noticed that if I change the loop code to the following



the code appears to be followed without missing steps. Could somebody explain this to me please?

Thanks again

Jack

EDIT 2***

I have also uploaded all of the java files to the following link. If this is not allowed on the website please let me know and I will both remove the link and not do it again.

Java Docs

Thanks, Jack




 
Saloon Keeper
Posts: 21967
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't want to do it that way. That loop will eat all your CPU time doing virtually nothing.

A "splash screen" is often just an overlay window/dialog that is presented, remains for a set period of time, then goes away. In desktop GUI systems, "go away" usually means becomes invisible, after which time the presentation objects may be destroyed.

There's no such concept as "app loading" for a desktop app, since the app is already there and it loads before it begins. You'd only "load" if you were getting additional resources. However complex apps - such as the Eclipse IDE - may have many subsystems to initialize, and it's not uncommon in such cases to keep the splash screen up while that happens. Usually you cannot use the app until those systems are have finished setting up anyway. As a side benefit, a progress bar and/or updating text display indicating the resource being initialized may be presented on the splash screen.

So, in short, a splash screen is something that you would present that would either wait - NOT loop! - on a timer, or it would wait for background processes to complete. The operative word in both cases is "wait".
 
J Thompson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ok, I'll look into the use of timers instead then.

Thank you

Jack
 
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:There's no such concept as "app loading" for a desktop app, since the app is already there and it loads before it begins. You'd only "load" if you were getting additional resources.



If you do use the built-in splash screen feature in Java for a Swing application, what happens is that the splash screen is displayed until the application's main JFrame appears. This can take a second or three to happen if (as you say) there are resources to be set up. (I just tried it to see how it actually behaved. It takes a couple of seconds for my application.)

So I suppose you'd want to use Jack's version if your code displayed the main JFrame and then spent some time setting things up. But this is a fairly well-known Swing situation: you want things to happen in the background while the contents of the GUI are static. The tutorial for Swing threading is here: Lesson: Concurrency in Swing.

Note that the tutorial explains how your background task can update the GUI as it goes along, in case you wanted to display e.g. a list of tasks being performed as part of the setting-things-up process.
 
Tim Holloway
Saloon Keeper
Posts: 21967
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I wouldn't use Jack's version.

That loop will consume 100% CPU after "SplashPanel" is initialized. Or at least it would if it wasn't busy filling stdout with Apploading messages. We don't do CPU loops in the multi-tasking world. Except for OS spin locks. If you want to consume endless CPU cycles, give them to folding@home or something more useful or entertaining.

I should have noted that giving members and methods names that start with uppercase letters is also a no-no, but the CPU loop screamed at me.
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:No, I wouldn't use Jack's version.



I wouldn't either. I translated Jack's idea of a spin-lock into something which would run in a background thread but using a standard technique instead of a DIY.

And I have a question: This is in the Android forum but we seem to be talking as if it's a Swing application. Which is it?
 
Tim Holloway
Saloon Keeper
Posts: 21967
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:
And I have a question: This is in the Android forum but we seem to be talking as if it's a Swing application. Which is it?



Because apparently, I'm becoming increasingly blind and mis-read the forum ID.

Still, the same rules and principles would apply in Android as on Swing desktop application. In fact, loop-waiting on an Android device might even be worse.
 
J Thompson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Tim Holloway wrote:No, I wouldn't use Jack's version.



I wouldn't either. I translated Jack's idea of a spin-lock into something which would run in a background thread but using a standard technique instead of a DIY.

And I have a question: This is in the Android forum but we seem to be talking as if it's a Swing application. Which is it?



Sorry, I think that was my mistake, I attempted to post this topic into Beginning Java, I didn't realize that this was a swing subject.

I have a question here as well, what do you mean by spin lock?

Tim Holloway wrote:
I should have noted that giving members and methods names that start with uppercase letters is also a no-no



This sounds blunt, but its not meant to be, why?

Thanks, Jack
 
Tim Holloway
Saloon Keeper
Posts: 21967
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're used to mis-filed questions. No problem.

Java has built-in constructs for multi-threading, unlike most of the older programming languages. Modern-day operating systems are themselves multi-threaded, as well. Generally not only with multiple concurrent tasks, but multiple CPU cores to run those tasks simultaneously. To juggle many resources with synchronization requirements, the OS uses something called a "lock", which grants access to a critical resource to one and only one thread. These locks are sometimes arranged in tiers, where to get a higher-level lock, you first have to acquire one or more lower-level locks and the whole thing is best addressed by a good book on operating systems designs, because it's too much to handle here.

Outer locks are generally like Java "synchronized" locks. A task requests the lock and if another task already has it, then the task making the request is placed in a holding queue, usually first-in/first-out (FIFO). But some resources are more critical and should only be held for a few micro-seconds at most and cannot afford the overhead to be put into a waiting state. For these resources, we use a "spin lock". Spin locks are usually built on special CPU instructions that claim a specific byte (or more) in memory and have hardware interlocks that make sure that only one CPU at a time can write to them. So a very tight loop "spins" around that asset, waiting for the current owner to give it up. The usual injunction against CPU-bound loops does not apply here, since the wait and the use of the lock are both expected to be so short as to have no material effect on the overall CPU workload. It's a fascinating topic, but it's pretty far outside the scope of normal Java applications programming.

As to capitalization, Java has a convention that class names start with an upper-case letter, but class property names, method names, and other lesser components begin with a lower-case letter. The compiler doesn't care, but it's both a visual indication for the programmer and in some cases, Java tools expect this convention to be followed. For example, JavaBeans typically have private properties with public access methods. So, for a property named "startTime", the convention is that you can access it via methods named "getStartTime()" and "setStartTime()". Note how the get/set methods capitalize the property name so as to highlight it better, though. Boolean properties may replace "get" with "is", for you might have "isStarted()".
 
J Thompson
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Makes sense, thank you everybody for your help.

Incase somebody else is reading this thread, I managed to make the program work as expected using the Timer method. Again, thank you all for the help
 
today's feeble attempt to support the empire
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic