In a java class i have two timers
TimerTask t1 = new TimerTask() {.. }
TimerTask t2 = new TimerTask() { ...}
Do t1 and t2 execute as two separate threads, how do you ... |
I have a logging code which needs to be executed after all threads are executed.
Thread t1 = new MyThread();
Thread t2 = new MyThread();
t1.run();
t2.run();
doLogging();
Is there any way to execute doLogging() only ... |
I have no idea why this is hanging. I'm trying to capture output from a process run through commons-exec, and I continue to hang. I've provided an example program to demonstrate ... |
I have developed a Java Swing application, which uses the SwingWorker class to perform some long running tasks. When the application is run from the IDE (Netbeans), I can start multiple ... |
In fact I would execute a specific task( a set of instructions) for a determined period.
For example : I want my program to execute the task for 5 minutes, if it ... |
I'm using net beans for Java development. I'm working on a multi threading application and I want to easily identify code sections which are executed by more than one thread? Is ... |
I have a low latency system that receives UDP messages. Depending on the message, the system responds by sending out 0 to 5 messages. Figuring out each possible response ... |
|
I am working on an assignment and have to create two classes, one represents a person, and the other representing a bridge. Only one person can be "crossing" the bridge at ... |
I'm new in java, so i guess it's a simple problem. In my event listener i have a method which declares variables and i'm trying to use those variables in thread ... |
I'm working on making an interface for a robot. My Robot class has methods that include movement, stopping movement and reading sensor data. If at all possible, I'd like to have ... |
I'm about to learn the java threading facility.
I have 2 classes:
public class Main {
public static void main(String[] arg) throws Exception {
...
|
I'm working with threads but after a time, most of them stop doing their job. The first thing I thought was a deadlock, but all are with state RUNNING.
I suppose there ... |
I want to run an external programs repeated N times, waiting for output each time and process it. Since it's too slow to run sequentially, I tried multithreading.
The code looks ... |
I have met a Out of Memory problem in my application.
In one of my threads execution, there is a sudden java.lang.OutofMemoryError and make that threads die.
I have already select the memory ... |
I have a lot of data in a database(PostgreSQL) and need to process all. My program have threads to process all these data and follows these logic.
- Get a part of data ...
|
If int incrementing/decrementing operations are not atomic in Java 6, that is, they are said to be performed in several steps (read the value, increment, write etc), I would like to ... |
public class Sample extends Panel {
// Constructor
public Sample() throws IOException {
/* JobOne and JobTwo need to be implemented using Thread at same ...
|
I am using the JavaMail API , and there is a method in the Folder class called "search" that sometimes take too long to execute. What i want is to execute ... |
Many time-consuming threads (500-900ms.) are created in the application.
They are to be executed in the order they have been created - one after another - not simultaneously. The execution should ... |
This were the only two questions I couldn't answer in the interview I got rejected from last night.
|
I'm building an HTTP Proxy in node.js. When the incoming request meets some conditions, a long running job is executed. When this happens, all the subsequent requests must wait for the ... |
I have two Threads classes "AddThread" and "ReadThread". The execution of these threads should be like this "AddThread should add 1 record and wait until ReadThread displays the record after that ... |
I need to execute an action after a specific amount of time (for example 30 minutes after the app started up, if the app is still up).
What are my options and ... |
As you know, trading strategies take actions based on real time feed, such as when the bid or the last trade price changes. A data feed provider streams quotes to our ... |
I have an application which uses threads to run heavy tasks. When the thread ends its own task, it creates a file and sends a message using System.out.println().
In Eclipse works fine, ... |
The question is confusing, but thats what i want to do:
public class Main
{
MyClass instance = new MyClass();
Thread secondThread = new Thread(instance);
...
|
I was asked this question in an interview - not sure if it makes sense.
You have several threads of same priority started and running, how do you make sure that a ... |
Does this imply that without synchronization, method one could return before the expressions i++ and j++ are evaluated? I assume that you are refering to this code: class Test { static int i = 0, j = 0; static void one() { i++; j++; } static void two() { System.out.println("i=" + i + " j=" + j); } } Since methods ... |
The join() method doesn't return until after the thread you call it on has exited. Generally you use join() to wait for a thread to finish, and only then continue on. So in your program, t2 isn't started until t1 is complete. Simply move the first join() call after the second start() call, so you start both threads before joining either ... |
Hi, I was trying to invoke Operating System commands from Java and I can have a simple Java class to do that, but I would like to make use of Java Threads to do this in case of the called exe that doesn't responds for a while and would like to terminate the call. Any help in this would be greatly ... |
You can have a try/catch inside the run() method to handle the exceptions in that second thread, or you can use a callback of some kind. A callback is a method that the second thread can call to report its status. For example, something like Object m_result; void setResult(Object o) { m_result = o; } ... public void run() { try ... |
In other words, how would you execute methods in an existing thread when you won't know until runtime which method(s) need executing or n which order and it will be determined by a different thread? What I'm working on is an XML-RPC server using Apache's implementation. I'm working with COBOL files and in order to open a new connection to a ... |
This is a frequently-asked question. It may even actually be in the JavaRanch FAQ. Anyway, there is no safe way forcibly to stop another thread. You must not, however tempting it may seem, use the deprecated methods like Thread.stop(), because your application will foul-up (not every time you run it, but intermittently and confusingly). You have to devise a way by ... |
Hey everyone, Heres my situation: I'm already extending Applet so extending Thread is not an option. I've already used the "Run()" method in the class i've been coding, but i need to run another thread which does something completely different: (Checks to see if a txt file has been changed every 60 seconds) -- since i can't have a second "Run" ... |
p. 35, Java Threads: ".. any thread that has a reference to another thread can execute any method of that other thread's object." class a extends Thread{ public a(){ super(); } public void run(){ if(doSomething)return; else return; } boolean doSomething(){ while(true){ noOp;// no operation, just keep the // processor busy for a test case. } return true; } } class b ... |
Hi, An enhancement I'm working on requires me to execute sequential lines of time-consuming code in a separate thread so that the user has the ability to terminate that particular task. Now, since I an not using a loop, I cannot use the time-tested "checking- for-a-flag" method to terminate the thread. Also, the javadocs recommend not using the stop() or suspend() ... |
Hi! Question: I have a JTree with Page objects. When a user rights click the page and chooses "Create Preview" and method is executed in another class. This method starts a thread. Another application will create previews etc so I have to wait for the previews to be done in the thread. If the user clicks create preview on the same ... |
Hi, I need to submit a job in it's own thread, but cancel the job (and kill the thread) if the job takes more than 5000ms to complete. I'm attempting to achieve this by using the facilities in java.util.concurrent like this: private void submitJob() throws Exception { // Submit the job for execution Runnable job = new MyRunnable(); ExecutorService executor = ... |
Hi, I like to execute a external exe file from my java code.I like to provide some input data like username ,password to that running subProcess.Here is my code: String line=null; OutputStream stdin = null; InputStream stderr = null; InputStream stdout = null; try{ Process process = Runtime.getRuntime().exec(); stdin = process.getOutputStream (); stderr = process.getErrorStream (); stdout = ... |
hi, After executing this code it gives me output : Ex-B Exception in thread "main" java.lang.IllegalMonitorStateException: current thread not owner at java.lang.Object.notifyAll(Native Method) at tA.main(tA.java:35) can anybody please explain me why? please correct my understanding explained below: 1- At commented // 1 main thread creates a new thread and makes it wait at the thread pool 2- At commented // 2 ... |
|
|
I am trying to accomplish two things: 1. I am running cygwin on Windows7 to execute my unix shell commands and I need to automate the process by writing a Java app. I already know how to use the windows shell through Java using the 'Process class' and 'Runtime.getRuntime().exec(cmd /c dir)'. I need to be able to do the same with ... |
Can any one tell me Which of the following statements are true 1. To determine which thread has to execute set the priority code of the thread. 2. Java uses a time-slicing scheduling system to determine which thread to execute. 3. Java uses a pre-emptive, co?operative system to determine which thread will execute. 4. The scheduling in Java is platform dependent, ... |
Hi We have a Java program that reads a text-file and then put it into the database (into four different tables). We make use of ADF BC 10g for the interaction with the database. We make use of threads in a threadpool. The Java program consists of different methods, for each insert/update into the table there is an own method with ... |
- User runs MainGUIWindow - User presses ManualConfigurationButton - A Test Window pops up. - The TestWindow runs the first test which is to receive a ShortMessage from a MIDI device. The message is never sent until the user hits something on the MIDI device to send the message. A ShortMessage basically contains which piece of the MIDI device sent the ... |
|
I'd argue that it's better to think of the objects that happen to have threads associated with them, rather than threads that happen to have objects associated with them. And that the way you'd do this would be to have a method on the target object that would add an instruction to a queue. Then arrange it so that the thread ... |
I have a situation in which I have 3 threads(say a,b,c) executing a method (say execute()). Is there any way by which I can make thread b to execute the method execute() after thread a and c (these two can execute in any order), i.e can I make thread b to execute the method only at the end(after a and c ... |
|
no. the same amount of work still has to be done. this would probably actually slow things down, thanks to the expense of creating and managing the threads. say you do this work with 5 threads. each thread will indeed (roughly) handle 20% of the work. however, they don't really run concurrently, they only appear to. you might in theory see ... |
public static void main(String argv[]) { new BackUpServer(); new DiscoverServer(); // etc etc } } Each of these servers is a thread (they all have Thread t = new Thread(this) and t.start() in their constructors). The problem now is that when I do java Main from the command prompt, all the servers are doing their work and displaying output on the ... |
|
public void run(){ try{ FileWriter fs1 = new FileWriter("/home/crito/tt.txt"); FileWriter fs2 = new FileWriter("/home/crito/ss.txt"); BufferedWriter bw1 = new BufferedWriter(fs1); BufferedWriter bw2 = new BufferedWriter(fs2); for (int tk = 0; tk<=1000; tk++) { double m = Math.sqrt(tk); if (Thread.currentThread().getName() == "Alpha") { bw1.write("Square root of "tk" = "+m); bw1.newLine(); } else { bw2.write("Square root of "tk" = "+m); bw2.newLine(); } } bw1.close(); ... |
The scheduler has its own rules for switching threads. There's probably a timeslice involved, but it has to be big enough to make it worth the overhead of switching. The idea that the scheduler knows what a "step" is in a given thread, and lets each thread take one "step" is completely mistaken. In addition to timeslices, a major reason for ... |