|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On November 18 2014 00:09 BByte wrote:Show nested quote +On November 17 2014 21:25 goody153 wrote: I was specifically tasked to convert 2 websites that was hardcoded into MVC coding style(or architecture or whatever the term i should use) so i did some googling and i ended up finding 3 PHP framework that apparently commonly used and i believe would help me on the task which are CakePHP, Laravel and CodeIgniter.
I won't ask which one is the best since they all probably have their own pros and cons. So my question would be which PHP framework between CakePHP, Laravel and CodeIgniter have you guys used or have been using ? or have you switched from one framework to another ? or which framework have you had a wonderful experience or have worked on you? which didn't ?
I haven't used any of the mentioned frameworks extensively, I use Symfony both at work and for some of my own projects. Based on what impressions I have of the three mentioned, I'd choose Laravel. The best framework really depends on what you actually need to convert those sites. If they are rather simple, you may want to consider using a micro-framework. Even something simple will force you to use a proper MVC model and micro-frameworks can be a lot easier to learn. You certainly don't have to use or learn to use every component even if you pick a heavier option though. When converting an existing site I'd especially consider support for different types of template and configuration files. If the sites are currently hardcoded, it's likely the view / template code is written in raw HTML + PHP. You may want to keep it that way and just separate the business logic. But if you also need to create some new templates a proper template engine can be useful. Mixing different types of templates may not be the cleanest solution, but in can mean a lot less work. Same goes for config files: Maybe you want to keep some of the old PHP or XML files, but want to use YAML for the new files. If the framework can seamlessly support different types of configuration, it's a little easier.
I'm also using Symfony (and now Kohana) and would also suggest Laravel. CodeIgniter is nice but it's rather dated and hasn't been developed for years so it might not adhere to the more modern approach and evolution of PHP itself. If you're new to the MVC pattern (that's what you call it) I'd suggest trying it out first in mini (formerly known simply as php-mvc) which is a micro-framework that let's you get the gist of it in 30 minutes or so. It's pretty bare bones but that's good, you'll learn how things work and it'll be much easier for you to jump to full-stack frameworks like Laravel or Symphony which offer a much higher level of abstraction. Because they offer all those abstraction layers you might find yourself doing stuff without actually knowing or fully understanding what's going on. I don't know about mini but php-mvc also had an "advanced" fork where you got SASS support and Twig templating engine.
I used to really hate Symfony, but after trying out some other frameworks I'm actually missing it in my new job (except for Sonata and CMF parts of it, I still despise them).
|
I can't get JSTL into my jsp. I am using this include statement which seems to work for everyone else on the internet.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
I am using maven and have the javaee-web-api dependency in my project and on my build path. I can see the JSTL packages inside the jar but my include statement just won't find them and I do not know why. It does work if I get the JSTL dependency specifically but that just makes wonder why they bundled the entire web api into one package if I can't actually use the stuff inside of it. I am sure I am missing something obvious ><
|
On November 18 2014 07:24 Manit0u wrote:Show nested quote +On November 18 2014 00:09 BByte wrote:On November 17 2014 21:25 goody153 wrote: I was specifically tasked to convert 2 websites that was hardcoded into MVC coding style(or architecture or whatever the term i should use) so i did some googling and i ended up finding 3 PHP framework that apparently commonly used and i believe would help me on the task which are CakePHP, Laravel and CodeIgniter.
I won't ask which one is the best since they all probably have their own pros and cons. So my question would be which PHP framework between CakePHP, Laravel and CodeIgniter have you guys used or have been using ? or have you switched from one framework to another ? or which framework have you had a wonderful experience or have worked on you? which didn't ?
I haven't used any of the mentioned frameworks extensively, I use Symfony both at work and for some of my own projects. Based on what impressions I have of the three mentioned, I'd choose Laravel. The best framework really depends on what you actually need to convert those sites. If they are rather simple, you may want to consider using a micro-framework. Even something simple will force you to use a proper MVC model and micro-frameworks can be a lot easier to learn. You certainly don't have to use or learn to use every component even if you pick a heavier option though. When converting an existing site I'd especially consider support for different types of template and configuration files. If the sites are currently hardcoded, it's likely the view / template code is written in raw HTML + PHP. You may want to keep it that way and just separate the business logic. But if you also need to create some new templates a proper template engine can be useful. Mixing different types of templates may not be the cleanest solution, but in can mean a lot less work. Same goes for config files: Maybe you want to keep some of the old PHP or XML files, but want to use YAML for the new files. If the framework can seamlessly support different types of configuration, it's a little easier. I'm also using Symfony (and now Kohana) and would also suggest Laravel. CodeIgniter is nice but it's rather dated and hasn't been developed for years so it might not adhere to the more modern approach and evolution of PHP itself. If you're new to the MVC pattern (that's what you call it) I'd suggest trying it out first in mini (formerly known simply as php-mvc) which is a micro-framework that let's you get the gist of it in 30 minutes or so. It's pretty bare bones but that's good, you'll learn how things work and it'll be much easier for you to jump to full-stack frameworks like Laravel or Symphony which offer a much higher level of abstraction. Because they offer all those abstraction layers you might find yourself doing stuff without actually knowing or fully understanding what's going on. I don't know about mini but php-mvc also had an "advanced" fork where you got SASS support and Twig templating engine. I used to really hate Symfony, but after trying out some other frameworks I'm actually missing it in my new job (except for Sonata and CMF parts of it, I still despise them). Ok i'll try this php-mini first. Thanks .
On November 18 2014 00:09 BByte wrote:Show nested quote +On November 17 2014 21:25 goody153 wrote: I was specifically tasked to convert 2 websites that was hardcoded into MVC coding style(or architecture or whatever the term i should use) so i did some googling and i ended up finding 3 PHP framework that apparently commonly used and i believe would help me on the task which are CakePHP, Laravel and CodeIgniter.
I won't ask which one is the best since they all probably have their own pros and cons. So my question would be which PHP framework between CakePHP, Laravel and CodeIgniter have you guys used or have been using ? or have you switched from one framework to another ? or which framework have you had a wonderful experience or have worked on you? which didn't ?
I haven't used any of the mentioned frameworks extensively, I use Symfony both at work and for some of my own projects. Based on what impressions I have of the three mentioned, I'd choose Laravel. The best framework really depends on what you actually need to convert those sites. If they are rather simple, you may want to consider using a micro-framework. Even something simple will force you to use a proper MVC model and micro-frameworks can be a lot easier to learn. You certainly don't have to use or learn to use every component even if you pick a heavier option though. When converting an existing site I'd especially consider support for different types of template and configuration files. If the sites are currently hardcoded, it's likely the view / template code is written in raw HTML + PHP. You may want to keep it that way and just separate the business logic. But if you also need to create some new templates a proper template engine can be useful. Mixing different types of templates may not be the cleanest solution, but in can mean a lot less work. Same goes for config files: Maybe you want to keep some of the old PHP or XML files, but want to use YAML for the new files. If the framework can seamlessly support different types of configuration, it's a little easier.
What i'm trying to convert is raw HTML + PHP coded and i would like to use majority of the codes from it since i don't want to recreate most of the functions.
Alright people has also been saying Symphony or Laravel so i'll try both and get the feel which i would like to use. And i won't be using codeigniter since people said that easier but there is no support behind it anymore.
Thank guys.
|
Why are so many developers using MacBooks? Are they that good or is it just a trendy thing to do? I'm asking because I need a new laptop for work and am seriously considering giving it a shot even though I despise Apple products...
|
My guess is that MacBooks are premium devices (good keyboard and touchpad, good screen, robust, good battery life etc..) and Mac OS X is POSIX compliant, which makes macbooks very usable in a Unix development setting. There are also a few productivity goodies on macbooks, like fancy multitouch touchpad gestures and such.
But they could probably do the same shit they do with a computer half as cheap. Apple's marketing and brand loyalty is just that good.
|
Solid product, and usually someone else (your company) is paying.
|
On November 18 2014 08:38 Azerbaijan wrote:I can't get JSTL into my jsp. I am using this include statement which seems to work for everyone else on the internet. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
I am using maven and have the javaee-web-api dependency in my project and on my build path. I can see the JSTL packages inside the jar but my include statement just won't find them and I do not know why. It does work if I get the JSTL dependency specifically but that just makes wonder why they bundled the entire web api into one package if I can't actually use the stuff inside of it. I am sure I am missing something obvious ><
What container are you deploying to? How are you declaring your dependency for the javaee-web-api and what version?
|
On November 20 2014 11:29 phar wrote: Solid product, and usually someone else (your company) is paying.
The thing is, I have to buy it myself for my personal use. Just found out I can get just as good MSI laptop for half the price.
|
On November 20 2014 11:41 Manit0u wrote:Show nested quote +On November 20 2014 11:29 phar wrote: Solid product, and usually someone else (your company) is paying. The thing is, I have to buy it myself for my personal use. Just found out I can get just as good MSI laptop for half the price. MSI and ASUS make good laptops for a good price, if you want something with a bit more power (especially graphics-wise).
There's a lot more options when it comes to extremely barebones machines.
|
Hey guys, I was wondering how the elasticsearch option "mlockall" to prevent JVM from using heap memory is actually implemented.
Also was wondering - at work they have some elasticsearch nodes (I don't work on them, I just do monitoring T_T) but I was looking into the boxes for some issue and was seeing few things I had question on
1) JVM were configured with mlockall but were still using swap (these are running on physical servers). Shouldn't they not be? 2) Servers have 100gb RAM and were at maybe 80gb usage, was seeing there was still some swap usage. Why would anything use swap space before ram gets filled (vm.swapiness set to 60 if thats reason)
|
On November 20 2014 16:40 teamamerica wrote: Hey guys, I was wondering how the elasticsearch option "mlockall" to prevent JVM from using heap memory is actually implemented.
Also was wondering - at work they have some elasticsearch nodes (I don't work on them, I just do monitoring T_T) but I was looking into the boxes for some issue and was seeing few things I had question on
1) JVM were configured with mlockall but were still using swap (these are running on physical servers). Shouldn't they not be? 2) Servers have 100gb RAM and were at maybe 80gb usage, was seeing there was still some swap usage. Why would anything use swap space before ram gets filled (vm.swapiness set to 60 if thats reason)
Ad. 2:
Swapping is necessary for two important reasons. First, when the system requires more memory than is physically available, the kernel swaps out less used pages and gives memory to the current application (process) that needs the memory immediately. Second, a significant number of the pages used by an application during its startup phase may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other applications or even for the disk cache.
Source: http://www.linux.com/news/software/applications/8208-all-about-linux-swap-space
|
|
|
I have a code question. The long and the short of it is that I don't fully understand threads in Java, or perhaps simply their interaction with applets. Here's a simplified version of the problem I'm having:
+ Show Spoiler +import java.applet.Applet; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class TestApplet extends Applet { public Button spawnThread; private class ButtonPresser implements Runnable {
public Button endThread; public TestApplet parent; public Object notifier = new Object(); public ButtonPresser(TestApplet parent){ this.parent = parent; } @Override public void run() { synchronized(notifier){ endThread = new Button("End Thread"); endThread.setBounds(0,100,100,20); endThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { synchronized(notifier) { notifier.notify(); } } }); parent.add(endThread); try { notifier.wait(); System.out.println("Thread is done!"); } catch (InterruptedException e1) { e1.printStackTrace(); } } } } public void init() { final TestApplet thisApp = this; spawnThread = new Button("Spawn Thread"); spawnThread.setBounds(0,0,100,20); spawnThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Thread t = new Thread(new ButtonPresser(thisApp)); t.run(); try { t.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } } }); add(spawnThread); } }
What I am attempting to do is spawn a thread that adds a button to an applet that will kill said thread. In order to do this, I am creating a button whose listener tells it to notify the object on which the thread's run method is currently waiting, thereby allowing the run() to complete and the thread to terminate. However, when I run this and click on the Spawn Thread button, the End Thread button is added, and then the applet completely locks up (accepts no clicks and can't be closed, have to kill the process). Can anyone explain to me why this is? I'm sure I'm doing something fundamentally stupid but am not sure what it is.
Also, I know no one uses applets anymore, that's what I see on every Stack Overflow question I read, but my guess is that this problem is not inherent to applets, but rather to my understanding. Also it seems I should be trying to use the java.util.concurrent library, but I'd like to know why this doesn't work before moving there.
|
On November 21 2014 06:18 raNazUra wrote:I have a code question. The long and the short of it is that I don't fully understand threads in Java, or perhaps simply their interaction with applets. Here's a simplified version of the problem I'm having: + Show Spoiler +import java.applet.Applet; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class TestApplet extends Applet { public Button spawnThread; private class ButtonPresser implements Runnable {
public Button endThread; public TestApplet parent; public Object notifier = new Object(); public ButtonPresser(TestApplet parent){ this.parent = parent; } @Override public void run() { synchronized(notifier){ endThread = new Button("End Thread"); endThread.setBounds(0,100,100,20); endThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { synchronized(notifier) { notifier.notify(); } } }); parent.add(endThread); try { notifier.wait(); System.out.println("Thread is done!"); } catch (InterruptedException e1) { e1.printStackTrace(); } } } } public void init() { final TestApplet thisApp = this; spawnThread = new Button("Spawn Thread"); spawnThread.setBounds(0,0,100,20); spawnThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Thread t = new Thread(new ButtonPresser(thisApp)); t.run(); try { t.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } } }); add(spawnThread); } }
What I am attempting to do is spawn a thread that adds a button to an applet that will kill said thread. In order to do this, I am creating a button whose listener tells it to notify the object on which the thread's run method is currently waiting, thereby allowing the run() to complete and the thread to terminate. However, when I run this and click on the Spawn Thread button, the End Thread button is added, and then the applet completely locks up (accepts no clicks and can't be closed, have to kill the process). Can anyone explain to me why this is? I'm sure I'm doing something fundamentally stupid but am not sure what it is. Also, I know no one uses applets anymore, that's what I see on every Stack Overflow question I read, but my guess is that this problem is not inherent to applets, but rather to my understanding. Also it seems I should be trying to use the java.util.concurrent library, but I'd like to know why this doesn't work before moving there.
Thread.join
When you call thread.join() from the main thread, it is now going to wait on the other thread to finish before it moves on.
You've created a deadlock. Thread a creates thread b. Thread b must be stopped by an action on thread a. However, thread a is currently waiting for thread b to finish before it can respond. Everyone loses.
Also, you shouldn't be calling thread.run(). You should call thread.start().
Starting a thread
|
On November 20 2014 16:40 teamamerica wrote: Hey guys, I was wondering how the elasticsearch option "mlockall" to prevent JVM from using heap memory is actually implemented.
Also was wondering - at work they have some elasticsearch nodes (I don't work on them, I just do monitoring T_T) but I was looking into the boxes for some issue and was seeing few things I had question on
1) JVM were configured with mlockall but were still using swap (these are running on physical servers). Shouldn't they not be? 2) Servers have 100gb RAM and were at maybe 80gb usage, was seeing there was still some swap usage. Why would anything use swap space before ram gets filled (vm.swapiness set to 60 if thats reason)
5 minutes of googling (so if its wrong, sorry..) shows that mlockall with the jvm is used through the lucid works java agent.
Lucid Works Java Agent
Are you guys following their recommended settings?
|
On November 21 2014 10:07 berated- wrote:Show nested quote +On November 21 2014 06:18 raNazUra wrote:I have a code question. The long and the short of it is that I don't fully understand threads in Java, or perhaps simply their interaction with applets. Here's a simplified version of the problem I'm having: + Show Spoiler +import java.applet.Applet; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class TestApplet extends Applet { public Button spawnThread; private class ButtonPresser implements Runnable {
public Button endThread; public TestApplet parent; public Object notifier = new Object(); public ButtonPresser(TestApplet parent){ this.parent = parent; } @Override public void run() { synchronized(notifier){ endThread = new Button("End Thread"); endThread.setBounds(0,100,100,20); endThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { synchronized(notifier) { notifier.notify(); } } }); parent.add(endThread); try { notifier.wait(); System.out.println("Thread is done!"); } catch (InterruptedException e1) { e1.printStackTrace(); } } } } public void init() { final TestApplet thisApp = this; spawnThread = new Button("Spawn Thread"); spawnThread.setBounds(0,0,100,20); spawnThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Thread t = new Thread(new ButtonPresser(thisApp)); t.run(); try { t.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } } }); add(spawnThread); } }
What I am attempting to do is spawn a thread that adds a button to an applet that will kill said thread. In order to do this, I am creating a button whose listener tells it to notify the object on which the thread's run method is currently waiting, thereby allowing the run() to complete and the thread to terminate. However, when I run this and click on the Spawn Thread button, the End Thread button is added, and then the applet completely locks up (accepts no clicks and can't be closed, have to kill the process). Can anyone explain to me why this is? I'm sure I'm doing something fundamentally stupid but am not sure what it is. Also, I know no one uses applets anymore, that's what I see on every Stack Overflow question I read, but my guess is that this problem is not inherent to applets, but rather to my understanding. Also it seems I should be trying to use the java.util.concurrent library, but I'd like to know why this doesn't work before moving there. Thread.joinWhen you call thread.join() from the main thread, it is now going to wait on the other thread to finish before it moves on. You've created a deadlock. Thread a creates thread b. Thread b must be stopped by an action on thread a. However, thread a is currently waiting for thread b to finish before it can respond. Everyone loses. Also, you shouldn't be calling thread.run(). You should call thread.start(). Starting a thread
Yes, the join() functionality is what I wanted, but I think I know where the problem is now. I had been forgetting to think about which thread deals with the actual ClickEvent, which presumably would be the main thread for applets. So then the main thread is waiting on the created thread to finish and can't jump out to execute ActionListener code for button presses? Time to go test this.
|
On November 21 2014 12:15 raNazUra wrote:Show nested quote +On November 21 2014 10:07 berated- wrote:On November 21 2014 06:18 raNazUra wrote:I have a code question. The long and the short of it is that I don't fully understand threads in Java, or perhaps simply their interaction with applets. Here's a simplified version of the problem I'm having: + Show Spoiler +import java.applet.Applet; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class TestApplet extends Applet { public Button spawnThread; private class ButtonPresser implements Runnable {
public Button endThread; public TestApplet parent; public Object notifier = new Object(); public ButtonPresser(TestApplet parent){ this.parent = parent; } @Override public void run() { synchronized(notifier){ endThread = new Button("End Thread"); endThread.setBounds(0,100,100,20); endThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { synchronized(notifier) { notifier.notify(); } } }); parent.add(endThread); try { notifier.wait(); System.out.println("Thread is done!"); } catch (InterruptedException e1) { e1.printStackTrace(); } } } } public void init() { final TestApplet thisApp = this; spawnThread = new Button("Spawn Thread"); spawnThread.setBounds(0,0,100,20); spawnThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Thread t = new Thread(new ButtonPresser(thisApp)); t.run(); try { t.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } } }); add(spawnThread); } }
What I am attempting to do is spawn a thread that adds a button to an applet that will kill said thread. In order to do this, I am creating a button whose listener tells it to notify the object on which the thread's run method is currently waiting, thereby allowing the run() to complete and the thread to terminate. However, when I run this and click on the Spawn Thread button, the End Thread button is added, and then the applet completely locks up (accepts no clicks and can't be closed, have to kill the process). Can anyone explain to me why this is? I'm sure I'm doing something fundamentally stupid but am not sure what it is. Also, I know no one uses applets anymore, that's what I see on every Stack Overflow question I read, but my guess is that this problem is not inherent to applets, but rather to my understanding. Also it seems I should be trying to use the java.util.concurrent library, but I'd like to know why this doesn't work before moving there. Thread.joinWhen you call thread.join() from the main thread, it is now going to wait on the other thread to finish before it moves on. You've created a deadlock. Thread a creates thread b. Thread b must be stopped by an action on thread a. However, thread a is currently waiting for thread b to finish before it can respond. Everyone loses. Also, you shouldn't be calling thread.run(). You should call thread.start(). Starting a thread Yes, the join() functionality is what I wanted, but I think I know where the problem is now. I had been forgetting to think about which thread deals with the actual ClickEvent, which presumably would be the main thread for applets. So then the main thread is waiting on the created thread to finish and can't jump out to execute ActionListener code for button presses? Time to go test this.
Why do you want join? You shouldn't care on the thread that creates the second thread what the second thread is doing? You just want it running, yes? Join explicitly takes away the whole value that you created by offloading to another thread by rejoining them together.
|
On November 20 2014 11:41 Manit0u wrote:Show nested quote +On November 20 2014 11:29 phar wrote: Solid product, and usually someone else (your company) is paying. The thing is, I have to buy it myself for my personal use. Just found out I can get just as good MSI laptop for half the price. Yea I personally wouldn't spend the markup out of my own pocket. But then again, if I had no company phone and had to get my own phone, I'd be eyeing the $100 moto e.
|
On November 21 2014 12:27 berated- wrote:Show nested quote +On November 21 2014 12:15 raNazUra wrote:On November 21 2014 10:07 berated- wrote:On November 21 2014 06:18 raNazUra wrote:I have a code question. The long and the short of it is that I don't fully understand threads in Java, or perhaps simply their interaction with applets. Here's a simplified version of the problem I'm having: + Show Spoiler +import java.applet.Applet; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class TestApplet extends Applet { public Button spawnThread; private class ButtonPresser implements Runnable {
public Button endThread; public TestApplet parent; public Object notifier = new Object(); public ButtonPresser(TestApplet parent){ this.parent = parent; } @Override public void run() { synchronized(notifier){ endThread = new Button("End Thread"); endThread.setBounds(0,100,100,20); endThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { synchronized(notifier) { notifier.notify(); } } }); parent.add(endThread); try { notifier.wait(); System.out.println("Thread is done!"); } catch (InterruptedException e1) { e1.printStackTrace(); } } } } public void init() { final TestApplet thisApp = this; spawnThread = new Button("Spawn Thread"); spawnThread.setBounds(0,0,100,20); spawnThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Thread t = new Thread(new ButtonPresser(thisApp)); t.run(); try { t.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } } }); add(spawnThread); } }
What I am attempting to do is spawn a thread that adds a button to an applet that will kill said thread. In order to do this, I am creating a button whose listener tells it to notify the object on which the thread's run method is currently waiting, thereby allowing the run() to complete and the thread to terminate. However, when I run this and click on the Spawn Thread button, the End Thread button is added, and then the applet completely locks up (accepts no clicks and can't be closed, have to kill the process). Can anyone explain to me why this is? I'm sure I'm doing something fundamentally stupid but am not sure what it is. Also, I know no one uses applets anymore, that's what I see on every Stack Overflow question I read, but my guess is that this problem is not inherent to applets, but rather to my understanding. Also it seems I should be trying to use the java.util.concurrent library, but I'd like to know why this doesn't work before moving there. Thread.joinWhen you call thread.join() from the main thread, it is now going to wait on the other thread to finish before it moves on. You've created a deadlock. Thread a creates thread b. Thread b must be stopped by an action on thread a. However, thread a is currently waiting for thread b to finish before it can respond. Everyone loses. Also, you shouldn't be calling thread.run(). You should call thread.start(). Starting a thread Yes, the join() functionality is what I wanted, but I think I know where the problem is now. I had been forgetting to think about which thread deals with the actual ClickEvent, which presumably would be the main thread for applets. So then the main thread is waiting on the created thread to finish and can't jump out to execute ActionListener code for button presses? Time to go test this. Why do you want join? You shouldn't care on the thread that creates the second thread what the second thread is doing? You just want it running, yes? Join explicitly takes away the whole value that you created by offloading to another thread by rejoining them together.
Ok, so I'll just give a high-level picture of what I was hoping for. There's probably a proper paradigm for doing what I want to do, so that would also be welcome advice. Here's what I want:
main: Run some code Create a couple buttons, one of which is "Done tinkering" Run some more code based on previous button presses (including possible branches) More buttons, including "Done" button, etc.
So my goal was to spawn a new thread for all the buttons, which will terminate when I'm done messing with them, at which point the main thread continues on. Originally I was being even terribler, and passing the future code to be executed around inside Buttons, which would get sent an event when the "Done" button was pressed.
Basically, when "Done" is pressed, I would greatly prefer to back out one level to whatever set it up, rather than pass the future code into the "Done" creation to be executed on the press.
|
On November 21 2014 15:03 raNazUra wrote:Show nested quote +On November 21 2014 12:27 berated- wrote:On November 21 2014 12:15 raNazUra wrote:On November 21 2014 10:07 berated- wrote:On November 21 2014 06:18 raNazUra wrote:I have a code question. The long and the short of it is that I don't fully understand threads in Java, or perhaps simply their interaction with applets. Here's a simplified version of the problem I'm having: + Show Spoiler +import java.applet.Applet; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class TestApplet extends Applet { public Button spawnThread; private class ButtonPresser implements Runnable {
public Button endThread; public TestApplet parent; public Object notifier = new Object(); public ButtonPresser(TestApplet parent){ this.parent = parent; } @Override public void run() { synchronized(notifier){ endThread = new Button("End Thread"); endThread.setBounds(0,100,100,20); endThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { synchronized(notifier) { notifier.notify(); } } }); parent.add(endThread); try { notifier.wait(); System.out.println("Thread is done!"); } catch (InterruptedException e1) { e1.printStackTrace(); } } } } public void init() { final TestApplet thisApp = this; spawnThread = new Button("Spawn Thread"); spawnThread.setBounds(0,0,100,20); spawnThread.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Thread t = new Thread(new ButtonPresser(thisApp)); t.run(); try { t.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } } }); add(spawnThread); } }
What I am attempting to do is spawn a thread that adds a button to an applet that will kill said thread. In order to do this, I am creating a button whose listener tells it to notify the object on which the thread's run method is currently waiting, thereby allowing the run() to complete and the thread to terminate. However, when I run this and click on the Spawn Thread button, the End Thread button is added, and then the applet completely locks up (accepts no clicks and can't be closed, have to kill the process). Can anyone explain to me why this is? I'm sure I'm doing something fundamentally stupid but am not sure what it is. Also, I know no one uses applets anymore, that's what I see on every Stack Overflow question I read, but my guess is that this problem is not inherent to applets, but rather to my understanding. Also it seems I should be trying to use the java.util.concurrent library, but I'd like to know why this doesn't work before moving there. Thread.joinWhen you call thread.join() from the main thread, it is now going to wait on the other thread to finish before it moves on. You've created a deadlock. Thread a creates thread b. Thread b must be stopped by an action on thread a. However, thread a is currently waiting for thread b to finish before it can respond. Everyone loses. Also, you shouldn't be calling thread.run(). You should call thread.start(). Starting a thread Yes, the join() functionality is what I wanted, but I think I know where the problem is now. I had been forgetting to think about which thread deals with the actual ClickEvent, which presumably would be the main thread for applets. So then the main thread is waiting on the created thread to finish and can't jump out to execute ActionListener code for button presses? Time to go test this. Why do you want join? You shouldn't care on the thread that creates the second thread what the second thread is doing? You just want it running, yes? Join explicitly takes away the whole value that you created by offloading to another thread by rejoining them together. Ok, so I'll just give a high-level picture of what I was hoping for. There's probably a proper paradigm for doing what I want to do, so that would also be welcome advice. Here's what I want: main: Run some code Create a couple buttons, one of which is "Done tinkering" Run some more code based on previous button presses (including possible branches) More buttons, including "Done" button, etc. So my goal was to spawn a new thread for all the buttons, which will terminate when I'm done messing with them, at which point the main thread continues on. Originally I was being even terribler, and passing the future code to be executed around inside Buttons, which would get sent an event when the "Done" button was pressed. Basically, when "Done" is pressed, I would greatly prefer to back out one level to whatever set it up, rather than pass the future code into the "Done" creation to be executed on the press.
Thanks for the explanation and high level view, it makes things easier than just looking at a block of code. I'm by no means an applet expert, but, I'm going to try to restate what I believe you are trying to accomplish and the describe the approach I would take.
You want to create an applet with action buttons on it, we'll call them A, B, and C for short. When you click any of the buttons, you will add a Stop (A|B|C) button, as well as start the Action (A|B|C). Clicking the stop button will terminate the action.
If this is the case, I would have some sort of listener on buttons A, B, C like you do now. However, from looking at your code, I would change where the logic runs. For me, it makes most sense for the original button listener to - create the thread - create the stop button - create the stop button listener with a reference to the created thread - start the thread - add the button to the screen
Since the newly created button listener has reference to the newly created thread, the only thing you'll have to do click is to stop the thread. No reason for joins anywhere. The reason I would do it like this is from a separation of concerns part. The thread should be responsible for doing whatever background logic you want to be happening is. The main applet and button listeners are responsible for the coordination of the creation of threads and buttons.
|
|
|
|
|
|