is it legal for a thread to call this.start() inside its own constructor? and if so what potential issues can this cause? I understand that the object wont have fully initialized ... |
I am working on a mutual exclusion assignment, but when I started I noticed my application's thread ID's start at 9. It doesn't change when I compile and execute it again. ... |
I am trying to solve the collatz conjecture.
I am using HashMap and Vector classes. I have to iterate the loop 2 147 483 648 times, but after I store 8,438,409 values ... |
Well title says it, what is the difference between Executors.newSingleThreadExecutor().execute(command) and new Thread(command).start();
|
Why do we need to run the thread through start method and not directly through the run method ?
|
Ok , I know the two standard ways to create a new thread and run it in Java :
1 Implement Runnable in a class , define run method ,and pass an ... |
I was reading about the thread and found that we cant call start method twice on the same thread instance. But i didnt get the exact reason for the same. so ... |
|
Or is it?
I have a thread object from:
Thread myThread = new Thread(pObject);
Where pObject is an object of a class implementing the Runnable interface and then I have the ... |
Please explain the output of the below code:
If I call th1.run() ,the output is
EXTENDS RUN>>
RUNNABLE RUN >>
If I call th1.start() ... |
The threads should start at same split second. I understand, if you do thread1.start(), it will take some milliseconds before the next execution of thread2.start().
Is it even possible or impossible?
|
I need to implement thread.start() method in my java code. Please let me know through an example of overriding of thread.start() method and how it works?
|
I have several threads and one of them is running HTPP service using ProcessBuilder.
HttpThread class
ProcessBuilder pb = new ProcessBuilder(command);
Process process pb.start();
process.waitFor();
int exitValue = process.exitValue();
I would like to have another thread ... |
I have variable number of threads which are used for parallel downloading. I used this,
for(int i = 0; i< sth; i++){
thrList.add(new myThread (parameters));
...
|
I have a class Note and a class Meeting. There is an ArrayList named noteList in class Note. When an object of Meeting is created it is then registered in the ... |
Ok, I've done threading with Java before but something that I think should be correct is giving me this error
package com.mdog.tcpserver;
import java.net.*;
import java.io.*;
public class ServerDriver {
public ...
|
Sub-process in java are very expensive. Each process is usually support by a NUMBERS of threads.
- a thread to host the process (by JDK 1.6 on linux)
- a thread to read to read/print/ignore ...
|
If I use start() on a Thread object and the run() method returns, is it possible to call start() again?
eg,
MyThread myThread = new MyThread();
myThread.start();
// run method executes and returns in 2 ...
|
Folks,
I know this question has been asked before here, though indirectly. But it didn't answer my doubt.
Question : Is it legal to call the start method twice on ... |
Why we need to pass the runnable instance while creating the threads using the Runnable interface?
|
Is there a solution out there that can help on that? basically I want to build a Java app that could start a http server on a separate thread such that ... |
The following code works, fine, but i wonder .. conceptually, is it correct? Start the threads, wait for them to join. Should ThreadPool be used instead?
If possible, please comment
List<Thread> threads = ...
|
I'm trying to cleanup my tests by always resetting to a known state before each test. In JUnit it seems that the best way to do this is to have a ... |
I am doing a hello world on threads, i have created a simple thread with using the run() call (which it is just a normal method call) and a duplicate thread ... |
I wonder if invoking a thread start has the safe effect of updating a volatile or after acquiring a lock?
|
|
Why do we call the thread object's start() method which in turns calls run() method, why not we directly call run() method?
|
public class SieveGenerator{
static int N = 50;
public static void main(String args[]){
int cores = Runtime.getRuntime().availableProcessors();
int f[] = new int[N];
//fill array ...
|
I have a file. It sometimes happens that one thread has finsihed reading the file. The moment he is closing the reader another thread comes and try to read from the ... |
|
Hi Everyone, I have a simple question: Where does a waiting thread start after it got notified and obtained the lock from the monitor? Will it continue from where it stopped or start from the very beginning of the synchronized code block? Same question for the thread which get a chance to run after sleep() or yield() ... Thanks a lot ... |
hello folks. previously i post this message in java Biggener forum i got a answer but i want answer in more technical depth. //============================================================= it's nessecery to call start() method for thread which is indirectly calling the run() method.we can call run() method directly but that time it is multithreading why ? why is special about in calling start() method but ... |
I created a class X which implements runnable interface then created a object class ObjX then ObjX.start(). My question is I was expecting compiler error but it did run successfully. Since runnable interface has only one method run() and in my class X I did not declared start() method...so from where did it pulled from. |
I don't think it's usually a good idea. You might be alright if you take care to call start() at the end of the constructor. But, what happens when someone wants to subclass your class? The run() method might execute before the subclass constructor had finished, and access a partially initialised object. Not a good idea. Don't do it, and if ... |
Hi,all here is my problem, i expect 2 lines of "ok" on the screen,while i could only get 1, why? it seems the 2th start() doesnt work at all! public class ThreadTest extends Thread { public void run() { System.out.println("ok"); } public static void main(String[] args) { ThreadTest t = new ThreadTest(); t.start(); try { // wait first start() out cleanly ... |
|
I am working on a project and need the ability to start and stop a program that will be running 24/7, in case of problems. The company that did the 1st part of the project used start() and stop() to control their program, but mine is quite a bit different from theirs (plus I am still a beginner programmer). They have ... |
|
Because the Thread class represents an operating system thread (in a native thread JVM). You don't want the Thread to hold on to a valuable resource such as an OS thread until it is garbage collected, so the OS thread is junked once you exit the run() method. If you want to be able to restart your code, don't extend Thread, ... |
|
|
|
|
|
hi frnds, my name is kannan and i have a doubt which is bugging me for a long time - hope i get an ans here in this discussion group. i have read umpteen number of times that a thread should be run only through the start() method but no one has come out clearly as to y ?? my question ... |
A friendly place for programming greenhorns! Big Moose Saloon Search | Java FAQ | Recent Topics Register / Login JavaRanch Java Forums Java Threads and Synchronization Author Thread hung executing start() Angelo Primavera Greenhorn Joined: Sep 23, 2003 Posts: 3 posted Apr 27, 2004 08:45:00 0 Hi all, I have a strange and really not understandable ... |
start creates a new thread of execution for your thread to execute in, then the thread is scheduled to run and the run method is called by the system when it's ready to run your thread. start will return as soon as the thread has been spawned. calling run directly is the same as calling any other method, the calling code ... |
SO FAR I HAVE MANAGED THE COUNTER TO COUNT NUMBER OF LINES. BUT CANT USE IT IN MULTHITHREADING PLEASE HELP!!! This assignment asks you to write a program that will read all the files in a specified directory, count the number of lines in each file and output the results to another file named file_count_report. Your program should first get the ... |
Hi, A Thread(say, t) that has been issued a "t.start()" runs the thread once and completes the thread to 'dead' state. Now, when we again issue a "t.start()", there is no compiler error but a IllegalThreadStateException is thrown at runtime. My Questions are: 1. When the 'start' method calls 'run' internally, why does multiple 't.run()' methods when issued on the code, ... |
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ... |
I'd recommend asking this in the Struts forum since it's really a Struts-specific question. I believe there is a standard place in web.xml that you can specify a certain servlet to start with the server, and then use its init() method to do what you want. Note, however, that your particular container (for example, Tomcat) may not want you creating your ... |
Hello all, How can i start multiple threads ( number of threads to start is not defined)? For instance Thread t1 = new Thread("One"); Thread t2 = new Thread("Two"); t1.start(); t2.start(); t1.join(); t2.join(); In this case the main thread will wait for both t1 & t2 to exit the run method. Here i can code the way given above coz it ... |
|
It's true that you could override start() and do something useful before or after calling super.start(), but really, I'm sure the reason is that the guys who write Thread didn't think of it. Sun has always been loath to change existing APIs in ways that would break existing user code, and so if this wasn't final originally, they won't make it ... |
|
|
|
When your original thread calls the start() method, a new thread represented by the thread object is started. This new thread will then call the run() method, while your original calling thread has returned, and running something else. When your original thread calls the run() method, it acts just like any other method call. No new thread is started. It doesn't ... |
Your code starts running in one thread. If you create a GUI, that starts an event thread, and your event handlers are invoked on this thread, so that makes two. Aside from that, the JVM may create any number of additional threads for various purposes. I don't know how you got the numbers "1" and "2", but actually the number is ... |
Your understanding is correct. If you don't provide a runnable instance, then what you have to do is make a subclass of Thread and override run(). There's never a reason to create an instance of the Thread class itself without a Runnable. Thread's other constructors -- the ones that don't accept a Runnable argument -- should have been made "protected", so ... |
Hi, I've been trying to use threads, but I keep having problems. I found this bit of code and was wondering if I should be starting my threads within the public void run() int he main(String[] args) bit (underneath the createAndShowGUI). /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * ... |
Hi all, why we are calling threadInstance.start() method to run a thread even though we can invoke run() method directly by calling threadInstance.run() ...i have wrote one code and checked the output i observed one strange thing...it is, we can invoke run() method withod using start() method..... so what is the significance of using threadInstance.start() method over threadInstance.run() method.... I hope ... |
class MyThread implements Runnable { ... public void run() { // do something; } } class StartTwoThread { public static void main(String[] args) { MyThread t1 = new MyThread(..); MyThread t2 = new MyThread(..); t1.start(); t2.start(); } } Question -- I want to simulate t1 and t2 to simultaneously do something on a single object, but I just don't know how ... |
Hi, Welcome to JavaRanch! I don't believe there's a sound technical reason why they couldn't have made it possible to start a Thread again; it's just a simple model that works well in practice. The Thread class is implemented mostly in native methods; on modern JVMs these are implemented by calling out to the native platform's thread library. |
I have a Timer that runs as a deamon. To this Timer I add one single TimerTask to run each 60 sec. The Timer is instantiated by the init() method of a servlet at application startup. The problems is that the Timer starts TWO different threads with the TimerTask. Both threads run once every 60 sec, and simingly with about 5 ... |
Hi All, I want to start a thread when another thread has executed once and is sleeping. I am checking the state of the thread if it is in "Timed_wait" state I am starting the new thread. But in is this the opther thread is never started. I get the state as "Runnable" all the time. here is code snippet: Test ... |
Dear ranchar i having a code for a thread as below class First extends Thread{ public void run(){ System.out.println("I m in First...."); } } public static void main(String[] ar){ First p=new First(); Thread t=new Thread(p); t.start(); } } Well the above code will work fine as i have expected but tell me why we are calling the start method by Thread ... |
Have you checked out the Java Tutorial on Threads? It starts here. Basically, you can think of the thread as a worker that will do a task for you. The run() method is a list of instructions that you give the worker (before they start doing it). The start() method is your command that says "now, start doing it!". When you ... |
Here's what you can do instead. Put your code in a Runnable and run it on two threads: Runnable r = new MyRunnable(); Thread t = new Thread(r); t.start(); ... do something else .join(); t = new Thread(r); ... do something else t.start(); ... do something else t.join(); See how we executed the run() method in MyRunnable twice? Is that the ... |
The start() method doesn't create a new Thread. It puts the Thread object,on which it has been invoked, to ready or runnable state.It however creates an OS thread ,say T_OS. The run() method is best left to the JVM(the thread scheduler of JVM)for invocation. In other words, the client code instantiating a thread shouldn't place a call to the run() method ... |
70. start( ) coderanch.com |
i think/read, Every program import java.lang package which Provides classes that are fundamental to the design of the Java programming language.In java.lang package there is one interface called Runnable which description is "The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread." The Runnable interface should be implemented by any class whose ... |
Sample code: public class Server { ServerThread thread; private ServerThread extends Thread { public void run() { Server.this.run(); } } public Server() { if (thread == null) { thread = new ServerThread(); thread.start(); } } public void run() { .... } Can somebody explain for me? or give me some tips. I don't know why the author start the server like ... |
|
Hi All I have a problem scenario in which I have to start a thread from one class and then stop the thread from some other class. The approach i followed for this is: I have a static variable status in my thread class which I set to true in the class from where I want to start the thread. After ... |
Overriding start() in Threads When I override start method in a Thread created by extends Thread and a Thread created by implements Runnable there is a different reaction WHY? class Background extends Thread{ int i=0; public void run() { System.out.println(Thread.currentThread().getName()); }//End run public void start() { // super.start(); das wrde den Thread starten!!!!! System.out.println ("In start()"); } }//End class public ... |
I would interpret this question as expecting the public void run() to be chosen as the answer. When the system gets around to running the thead it starts execution in the thread's run() method. Calling start() justs tells the system that it can schedule the thread to be run at sometime in the future. -Barry |
|
Hello Vini, Every java program start with the main method, this it's the main thread. Each Thread has a call stack. In java the lifecycle of a thread begins when you call the start method. Like any other class you can call all the methods of Thread object but they will run in the same thread (even if you call the ... |
Hai All, I have come across the below exception. java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:597) at com..util.Handler.handleClient(Handler.java:262) at com..util.Handler.run(Handler.java:164) at java.lang.Thread.run(Thread.java:619) In the Handler.java class im creating another thread , and invoking that thread with start() method. I came across on the internet that,calling run() is a better way ,it might help resolving out of ... |
Hi, I'm studying the Thread area for the SCJP 6.0 exam and got a question that what is the difference between run() and start() of a thread instance? Are the both ways getting a new stack of thread ? For start(), what I understand is, the thread becomes runnable state, but how is run() ? Is it running directly on top ... |
I have one thread already running and it calls method which returns the true/false variable and depending on that returned boolean variable i want to call another thread . I want to pause the Parent thread for time till the second thread complete and then continue with the parent issue . Is there any best way . Please suggest . Thanks ... |
I have a singleton setup to run a thread. In the singleton, I check if the thread exists and create it if it doesn't it. I then call isAlive, and if it returns false, I call the thread start method. In the start method, I override the thread method to check if the thread isAlive, and if it's not, start it. ... |
|
If your unsure just always use start never call run directly .. When you call Start , run gets invoked in a new thread (multi threading) , call run directly and it will get invoked on your calling thread (single threading) you probably never want to do that and it confuses the reader (if you did want that effect but the ... |
public class R1 implements Runnable { public void run() { System.out.print(Thread.currentThread().getName()); } } public void run() { new Thread(new R1, "|R1a|".run(); new Thread(new R1(), "|R1b|").start(); System.out.print(Thread.currentThread().getName()); } public static void main(String[] args) { new Thread(new R2, "|R2|").start(); } } ...and the answer: (d) The program will compile without errors and will print out |R1b| once and |R2| twice, in some order, ... |
|
Hey everyone, I am currently working on a portion of a project using Threads and the Observer pattern, but I am running into some trouble with Threads. I am calling the start() method on my Thread but for some reason run() is never being called. According to the API for java.lang.Thread start() is supposed to make the JVM call the run() ... |
|
class ChildThread extends Thread { public boolean processing = false; public void run() { getResult(); } public void getResult() { connect with webservice and get some data from it. once it is successfully completed,Iam changing the flag value processing = false; } public void post(string params) { after getting parameters from calling method, i am starting this thread and setting flag ... |
Hi guys, I would like to know that if a thread has been started, it will run to completion? For example, in this code, will thread "Andi" always print its name? public class Race { public static void main(String[] args) { Horse h = new Horse(); Thread t1 = new Thread(h, "Andi"); Thread t2 = new Thread(h, "Eyra"); new Race().go(t2); t1.start(); ... |
Hi guys, Someone wrote me this about threads: "Thread running depends on the VM Scheduler and there is NO guaranteed a Thread will run and pick by the VM Scheduler despite you have started the Thread with a call x.start(). When you started the Thread it's just in a RUNNABLE STATE, reread the chapter until you fully understood" (Chapter 9 Thread ... |
|
|
|
|
Well, this aint school work. Was going through the docs and found that start method is not final. In my opinion, it should be. start() always starts executing the method in a new callstack so whats the use of giving the users to implement it in own ways? What if a user implements the method in its own way? Then what ... |
public static void main(String[] args) { TestThread a = new TestThread(); a.run(); System.out.print(a.i); a.start(); System.out.print(a.i); } } Select one option: A - Compiler error B - IllegalThreadStateException is thrown C - Prints "11" D - Prints "12" E - Prints "00" F - Prints "01" G - Prints "11" or "12" H - Print "00" or "01" I think that correct ... |
Generally, create an instance of Runnable that has a constructor (this can be a nested class or a nonpublic class or a full blown public class). In the constructor, initialize the state of the Runnable. If your thread is modifying any of that state, or creates additional state, add instance variables for these. Finally, when you want to "resume" the thread, ... |
Perhaps my question wasnt magnificient clear. i have already an application running over tomcat and i donot want to separate report generation classes from current running application. I want to just intiate thread at 23:59 and generate the report. And at very next day i can collect my reports. Currently iam not getting an idea how i should do it. |
|