I'm using java. I'm trying to execute a thread, but the issue I'm getting is
thread.start() method is getting executed, but as we know when we call the start method of thread, the run() method gets called internally.
But in my case the run() method is not getting executed:
public static void main(String[] args) throws Exception {
parseArguments(args);
ScraperStore scraperStore = ScraperStore.getInstance();
SocialSiteManager siteManager = new SocialSiteManager();
sitesToScrape = siteManager.getSocialSitesToScrape();
for (SocialSite site : sitesToScrape) {
ScrapeThread srThread = new ScrapeThread("srThread");
Thread scraper = new Thread(srThread);
srThread.setSiteToScrape(site);
srThread.setPageTypeToScrape(startPageToScrape);
srThread.setTypeToScrape(typeToScrape);
ArrayList<String> listOfValues = ScraperStore.getNextUrlToScrape(startPageToScrape, site);
srThread.setTypeToScrape(typeToScrape);
try {
srThread.setUrlOwnedBy(listOfValues.get(0));
srThread.setStartUrl(listOfValues.get(1));
scraper.start();
boolean state = scraper.isAlive();
scrapeThreads.add(scraper);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Thread class:
class ScrapeThread {
public ScrapeThread(String threadName) {
thread = new Thread(this,threadName);
System.out.println(thread.getName());
}
}
Run method:
public void run() {
try {
System.out.println("in the run method");
selenium = new DefaultSelenium(config.getHost(), Integer.parseInt(config.getPort()),
config.getBrowser(), config.getUrl());
selenium.start();
Integer count = 0;
while (startUrl != null) {
HtmlPage homePage = new HtmlPage();
homePage.setCreatedBy(new String());
homePage.setCreatedon(new String());
homePage.setModifiedBy(new String());
homePage.setModifiedOn(new String());
homePage.setNoOfItemsFound(new String());
homePage.setOwnedBy(urlOwnedBy);
homePage.setPageType(scraper.getPageTypeToScrape());
homePage.setPageUrl(startUrl);
proxy = getInitialisedProxy();
scraper.setNavigator(proxy.getNavigator());
scraper.setStartUrl(startUrl);
try {
scraper.initialize();
} catch (MyException e) {
if (status == false){
throw new Exception(MyException.NOTFOUND);
}
}
}
}
}
I'm using sellinium. Is there any chance that I'm getting the issue because of selenium?
Look at code and compare it with your code.
public static void main(String []args)
{
Runnable inst=new Runnable()
{
public void run()
{
System.out.println("Thread statement!");
}
};
Thread thrd=new Thread(inst);
thrd.start();
}
How did you come to know run method is not executed . didu u put a trace on the run method?
//Old
new Thread(niidleThread,"scraper"); scraper.start()
// new
new Thread(srThread); or
new Thread(srThread,"scraper");
Try the new one i have given above;
Just from a cursory review of your code... I see that you might have gone a little thread-happy. Consider:
ScrapeThread srThread = new ScrapeThread("srThread"); // This is creating your ScrapeThread object (which should really implement the Runnable interface)
Thread scraper = new Thread(srThread); // This is creating a thread which wraps another thread... take this out.
srThread.setSiteToScrape(site);
srThread.setPageTypeToScrape(startPageToScrape);
srThread.setTypeToScrape(typeToScrape);
ArrayList<String> listOfValues = ScraperStore.getNextUrlToScrape(startPageToScrape, site);
srThread.setTypeToScrape(typeToScrape);
try {
srThread.setUrlOwnedBy(listOfValues.get(0));
srThread.setStartUrl(listOfValues.get(1));
scraper.start(); // You would want to replace this with srThread.start(); once fixing the items I addressed above
boolean state=scraper.isAlive();
scrapeThreads.add(scraper);
}
catch (Exception e) {
e.printStackTrace();
}
http://www.javabeginner.com/learn-java/java-threads-tutorial might help you out a bit.
Related
I'm trying to get data from four different sources with four scanners. I do realize that I need to use threads. But here's the error message:
P.S = paths for the files were fine before using thread. ( I was using one file, path was ok.)
null
null
null
null
java.lang.ArrayIndexOutOfBoundsException: 41705
at getText.getCities(getText.java:132)
at getText$1.run(getText.java:23)
at java.lang.Thread.run(Unknown Source)
java.lang.ArrayIndexOutOfBoundsException: 41705
at getText.getNames(getText.java:112)
at getText$2.run(getText.java:30)
at java.lang.Thread.run(Unknown Source)
Code Itself
public class getText {
static int ln = 41705;
static String [] icaos = new String[ln];
static String [] iatas = new String[ln];
static String [] names = new String[ln];
static String [] cities = new String[ln];
public final Runnable typeA;
public final Runnable typeB;
public final Runnable typeC;
public final Runnable typeD;
public getText() {
typeA = new Runnable() {
public void run() {
getText.this.getCities();
}
};
typeB = new Runnable() {
public void run() {
getText.this.getNames();
}
};
typeC = new Runnable() {
public void run() {
getText.this.getIcao();
}
};
typeD = new Runnable() {
public void run() {
getText.this.getIata();
}
};
}
public static void main(String[] args) {
// TODO Auto-generated method stub
getText x = new getText();
new Thread(x.typeA).start();
new Thread(x.typeB).start();
new Thread(x.typeC).start();
new Thread(x.typeD).start();
System.out.println(icaos[32541]);
System.out.println(iatas[32541]);
System.out.println(names[32541]);
System.out.println(cities[32541]);
}
public void getIcao () {
try {
int i=0;
InputStream icao_stream = new FileInputStream("src/icao.txt");
Scanner icao_s = new Scanner(icao_stream);
icao_s.useDelimiter(",");
while(icao_s.hasNext()) {
icaos[i] = icao_s.next();
i++;
}
icao_s.close();
icao_stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getIata() {
try {
int i=0;
InputStream iata_stream = new FileInputStream("src/iata.txt");
Scanner iata_s = new Scanner(iata_stream);
iata_s.useDelimiter(",");
while(iata_s.hasNext()) {
iatas[i] = iata_s.next();
i++;
}
iata_s.close();
iata_stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getNames() {
try {
int i=0;
InputStream names_stream = new FileInputStream("src/names.txt");
Scanner names_s = new Scanner(names_stream);
names_s.useDelimiter(",");
while(names_s.hasNext()) {
names[i] = names_s.next();
i++;
}
names_s.close();
names_stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getCities() {
try {
int i=0;
InputStream cities_stream = new FileInputStream("src/cities.txt");
Scanner cities_s = new Scanner(cities_stream);
cities_s.useDelimiter(",");
while(cities_s.hasNext()) {
cities[i] = cities_s.next();
i++;
}
cities_s.close();
cities_stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
So the problem now is you have started your threads, but the control returns immediately to the main thread and your other threads are not finished working by then. What you need is to wait for those threads to finish working and then check for results. There are multiple ways to achieve that. A simple one is to change your main to:
public static void main(String[] args) {
getText x = new getText();
List<Thread> threads = new ArrayList<>();
threads.add(new Thread(x.typeA));
threads.add(new Thread(x.typeB));
threads.add(new Thread(x.typeC));
threads.add(new Thread(x.typeD));
threads.forEach(t -> t.start());
threads.forEach(t -> {
try {
//this makes the main thread to wait for thread "t" to finish
t.join();
} catch (InterruptedException e) {
// log or throw or anything you need to do
}
});
System.out.println(icaos[32541]);
System.out.println(iatas[32541]);
System.out.println(names[32541]);
System.out.println(cities[32541]);
}
This is not the most optimal solution but the simplest one. Others better options would be to use a CyclicBarrier or ExecutorService among others.
In the following code I create a callable which creates a Runnable inside the call()-method. My problem is that run()-method is never reached (code does not get executed). Do you know why and how to fix that?
public static void main(String[] args) {
Callable<Object> c = new Callable<Object>() {
#Override
public Object call() throws Exception {
Runnable r = new Runnable() {
#Override
public void run() {
System.out.println("hi");
}
};
return null;
}
};
try {
c.call();
} catch (Exception e) {
}
}
Callable<Object> c = new Callable<Object>() {
#Override
public Object call() throws Exception {
Runnable r = new Runnable() {
#Override
public void run() {
System.out.println("hi");
}
};
r.run();
return null;
}
};
try {
c.call();
} catch (Exception e) {
}
Do you know why...
Already explained by others: You have written code that creates a Runnable instance, but your code does not do anything with the instance after creating it. Your code does not directly call the run() method, nor does your code hand the instance off to any other object that would call the method.
...and how to fix that?
That would depend on what you want the code to do. There are simpler ways to write a program that prints "hi" if that's all you want.
It looks as if you are trying to learn something, but you haven't told us what you want to learn.
I'm trying to write a library for my users, and would like it to be as easy to use as possible. Right now, to use it, you have to start a new thread, and set a lot of variables to track, in your main class, however my users are not always experienced in Java, and threading might be too hard for some of my users.
So I want to move the entire threading into my class, and allow users to call a function, to start the thread.
Here is the thread I'm currently running in the main class:
int valueToAdd = 0;
DreamStatsTracker tracker = new DreamStatsTracker();
StartSessionResponse response = tracker.StartSession("ae-13s-90-11", "Abe");
System.out.println(response.getMessage());
System.out.println("Adding skill Herblore");
String message = tracker.AddSkill(response.getSessionId(), Skill.HERBLORE, "Abe");
Thread thread = new Thread(() -> {
while(true) {
tracker.SetValueForSkill(response.getSessionId(), Skill.HERBLORE, valueToAdd);
}
});
I would like to move that entire thing into the DreamStatsTracker class, and do something like this from the main class:
DreamStatsTracker tracker = new DreamStatsTracker();
tracker.AddSkill(Skill1, ValueForSkill1);
tracker.AddSkill(Skill2, ValueForSkill2);
tracker.Start();
This would start a new instance of the DreamStatsTracker, which would start a new thread, and track the values of the added skills continuously in the background, while the main class is running.
How would I approach this, and is it even possible?
I finally solved my issue, by hours of testing and trying.
This is what I did:
Tracker class:
Thread mainThread;
public DreamStatsTracker(){
mainThread = new Thread(() -> {
while(true){
for (SkillAndValue skillAndValue : Skills) {
try {
SetValueForSkill(this.SessionId, skillAndValue.getSkill(), skillAndValue.getValue());
System.out.println("Added value: " + skillAndValue.getValue());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void AddSkillAndValue(Skill Skill, Callable<Integer> function) throws Exception{
Skills.add(new SkillAndValue(Skill, function));
}
public void Start(){
for (SkillAndValue skillAndValue : Skills) {
AddSkill(this.SessionId, skillAndValue.getSkill(), this.Username);
}
this.mainThread.start();
}
This is the main class:
public static void main(String[] args) throws Exception {
DreamStatsTracker tracker = new DreamStatsTracker();
tracker.StartNewSession("ae-13s-90-11", "Abe");
tracker.AddSkillAndValue(Skill.HERBLORE, getRandomNumber());
tracker.Start();
}
private static Callable<Integer> getRandomNumber(){
return new Callable<Integer>(){
public Integer call(){
Random random = new Random();
return random.nextInt(10000);
}
};
}
The code in this question was clearly too long so I've written an example you can just copy and paste that I think encompasses the conceptual problem I'm having.
Thanks to those who read all the way through the previous version!
Two files
One calls the other and passes an ArrayList.
The 2nd file alters the ArrayList and provides a getter for the first file to access the altered file.
How can I make the first file wait for the processing in the second file, before calling the getter. At the moment this code gives you a NullPointerException because the first file isn't waiting.
join() on the thread of execution doesn't seem to work, and if I use wait(), how do I notify() to the first file from the second file?
Here's the code:
public class Launcher implements Runnable {
private ArrayList al = new ArrayList();
private ArrayProcessor ap;
public Launcher(ArrayList al){
this.al = al;
ArrayProcessor ap = new ArrayProcessor(al);
}
public static void main(String[] args){
ArrayList anArray = new ArrayList();
anArray.add(new Integer(1));
anArray.add(new Integer(13));
anArray.add(new Integer(19));
Launcher l = new Launcher(anArray);
l.liftOff();
}
public void liftOff(){
Thread t = new Thread(new Launcher(al));
synchronized(t){
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.ap = new ArrayProcessor(al);
System.out.println("the array: " + ap.getArray());
}
public void run() {
ap.doProcess(al);
}
}
and the called file:
public class ArrayProcessor extends Thread{
private ArrayList al;
public ArrayProcessor(ArrayList al){
this.al = al;
}
public void doProcess(ArrayList myAL){
this.start();
}
public void run() {
// this should increment the ArrayList elements by one
for (int i=0; i<al.size(); i++){
int num = ((Integer)al.get(i)).intValue();
al.set(i, new Integer(++num));
}
}
public ArrayList getArray(){
return al;
}
}
If you want one thread wait for another to finish you could use a CountDownLatch. Since there is a lot of code in your samples, here is a small POC:
public static void main(String[] args) throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
new Thread(new Runnable() {
#Override
public void run() {
try {
System.out.println("Thread doing some work...");
Thread.sleep(10 * 1000);
System.out.println("Thread done!");
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
System.out.println("Main Thread waiting...");
latch.await();
System.out.println("Main Thread can continue");
}
I'm trying to implement a piece of code to synchronously start looped service in Java. The idea is, code under // STARTER comment should be considered as piece of Service.go() method, so if service fails to start, I want to re-throw the exception synchronously. That piece of code should only finish in case I've tried to start the thread, waited until its execution flow reached some point and next, if there are no problems, my go() method quits and thread goes on, or, if there were problems, I can re-throw the exception caught in thread's run() method from my go() method. Here's the solution that seems to work fine, but I'm curious if it's possible to make it a couple times shorter :-)
public class Program {
private static boolean started;
private static Throwable throwable;
public static void main(String[] args) {
final Object startedSetterLock = new Object();
Thread thread = new Thread() {
public void run() {
System.out.printf("trying to start...\n");
boolean ok;
Throwable t = null;
try {
init();
ok = true;
} catch(Exception e) {
ok = false;
t = e;
}
synchronized(startedSetterLock) {
started = ok;
throwable = t;
startedSetterLock.notifyAll();
}
if(!ok) {
return;
}
while(true) {
try {
System.out.printf("working...\n");
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.printf("interrupted\n");
}
}
}
private void init() throws Exception { throw new Exception(); } // may throw
};
// STARTER
synchronized(startedSetterLock) {
thread.start();
try {
startedSetterLock.wait();
} catch(InterruptedException e) {
System.out.printf("interrupted\n");
}
}
// here I'm 100% sure that service has either started or failed to start
System.out.printf("service started: %b\n", started);
if(!started) {
throwable.printStackTrace();
}
}
}
And also, there's a reason to have initialization code executed within that thread, so, please, don't advise running initialization code explicitly in go() method and then just passing all the stuff to the thread.
Thanks!
How about overriding the Thread.start() method?
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
while (true) {
try {
System.out.printf("working...\n");
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.printf("interrupted\n");
}
}
}
#Override
public synchronized void start() {
try {
init();
} catch (Exception e) {
throw new RuntimeException(e);
}
super.start();
}
private void init() throws Exception {
throw new Exception("test");
}
};
t.start();
}