//The below code is throwing illegalmonitorstate exception.
public class Multithreading implements Runnable {
static int i=0;
public boolean ist1=true;
public boolean ist2=false;
public static void main (String args[]){
Multithreading ins= new Multithreading();
Thread t1 =new Thread(ins);
Thread t2 =new Thread(ins);
t1.setName("Even");
t2.setName("ODD");
t1.start();
t2.start();
}
#Override
// Wanted to right run method used by two threads to print
// even and odd number in sequence
public void run() {
while(i<=9){
try{
if(Thread.currentThread().getName().contains("Even")&& i%2==0){
System.out.println(Thread.currentThread().getName()+"________"+i);
i=i+1;
Thread.currentThread().wait(100);
}
// due to wait is not used with synchronized but
// i am not able to correct it
if(Thread.currentThread().getName().contains("ODD") && i%2>=1){
System.out.println(Thread.currentThread().getName()+"________"+i);
i=i+1;
Thread.currentThread().wait(100);
//System.out.println(e);
}
}catch(Exception e){
System.out.println(e);
}
}
}
}
To pause a thread, you need to use Thread.sleep() in your case instead of wait().
Related
public class TestSynchronization {
public static void main(String[] args) {
ThreadTest[] threads = new ThreadTest[10];
int i = 0;
for(Thread th : threads) {
th = new Thread(Integer.toString(i++));
th.start();
}
}
class ThreadTest extends Thread {
TestSynchronization ts = new TestSynchronization();
public /*synchronized */void run() {
synchronized(this) {
ts.testingOneThreadEntry(this);
System.out.println(new Date());
System.out.println("Hey! I just came out and it was fun... ");
this.notify();
}
}
}
private synchronized void testingOneThreadEntry(Thread threadInside) {
System.out.println(threadInside.getName() + " is in");
System.out.println("Hey! I am inside and I am enjoying");
try {
threadInside.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
I am not able to start the ThreadTest instances.
I expect that ThreadTest's run method be executed as soon as the line th.start(); is executed, the one inside main method.
When I run the program, I see niether my system.out nor any exception.
I debugged also, but could see loop runs 10 times.
You just started a Thread, not a ThreadTest. Thread's run() method does nothing. Instead, create and start() a ThreadTest.
for(ThreadTest th : threads) {
th = new ThreadTest(Integer.toString(i++));
th.start();
}
You'll also need a one-arg constructor in your ThreadTest class that will take the String you're passing to it.
public ThreadTest(String msg){
super(msg);
}
You'll also need to make the ThreadTest class static so you can access that nested class from the static main method.
static class ThreadTest extends Thread {
However, you'll wind up will all Threads waiting. As written, this code will call wait inside every Thread, but it will never get to notify. The notify method must be called on the Thread to be notified, from another Thread. If it's waiting, then it can never notify itself.
You have array of ThreadTest (thread) class which is not used.
I assume you wanted this:
public static void main(String[] args) {
ThreadTest[] threads = new ThreadTest[10];
int i = 0;
for(int i=0;i<threads.length;i++) {
threads[i] = new ThreadTest();
threads[i].start();
}
}
So, i apologize for the title. It's quite hard to explain in one sentence what i would like to do if you have no idea on how it is called.
So assume i can only use primitive thread functions (wait, notify, no concurrent package)
The program has 3 threads, all of them are the same and are called by the main thread. They behave normally until one of the three get an exception and so it must wait for the end of the remaining 2 threads in order to start a recovery process.
I was thinking about a static variable but I'm not really sure about it, i would love to keep it as simple as possible.
Each thread starts at the same time.
I don't see any reason why you can't use a static variable like you suggest. Here's how I would do it with an inner class...
private static boolean running = true;
public void test26546397() {
while (true) {
Thread t1 = new Thread(new MyRunnable());
Thread t2 = new Thread(new MyRunnable());
Thread t3 = new Thread(new MyRunnable());
t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
running = true;
// Do recovery
}
}
public class MyRunnable implements Runnable {
#Override
public void run() {
while (running) {
try {
// doStuff
} catch (Exception ex) {
running = false;
}
}
}
}
I would of course replace the while (true) with something a little more suitable.
I think you need java.concurrent.CountdownLatch, however if the java.concurrent package is not available to you can code this yourself using Object.wait/notify and synchronized blocks.
The latch can then be decremented in a finally {} on each Thread, this will be run if the Thread completes, or an exception occurs.
Your main program then just needs to wait for count to become 0.
public class StackOverflow26546397 {
static class CountdownLatch {
private int count;
private Object monitor = new Object();
public CountdownLatch(int count) {
this.count = count;
}
public void countDown() {
synchronized (monitor) {
count--;
monitor.notifyAll();
}
}
public void await() throws InterruptedException {
synchronized (monitor) {
while (count > 0) {
monitor.wait();
}
}
}
}
static class Job implements Runnable {
private CountdownLatch latch;
public Job(CountdownLatch latch) {
this.latch = latch;
}
#Override
public void run() {
try {
// do work.
Thread.sleep((long) (Math.random() * 3000d));
} catch (InterruptedException e) {
//
} finally {
latch.countDown();
}
}
}
public static void main(String[] args) throws InterruptedException {
CountdownLatch latch = new CountdownLatch(3);
new Thread(new Job(latch)).start();
new Thread(new Job(latch)).start();
new Thread(new Job(latch)).start();
latch.await();
System.out.println("All threads finished");
}
}
Not sure what you are trying to do but this is as simple as I can think of (just native concurrency):
Create a static or shared volatile boolean
private static volatile boolean exceptionOccured=false
Set the above to 'true' when exception occurs:
....}catch(Exception e){
exceptionOccured=true;
}
Check this periodically in you normal thread flow:
if (exceptionOccured)
//enter you synchronized call here
the synchronized method could look something like:
public synchronized void checkAndRecover(){
//decrement a counter or other logic to identify which is the last Thread and then
//perform any recovery logic
}
i need help, i try to use synchronization thread in java but it can't run...
i have two class with thread like this
for(int j=0;j<idW.length;j++){
webtext = d.getWebText(idW[j]);
ThreadPrepo tpo =new ThreadPrepo(webtext, host[j%jumhost], "server", 1099,idW[j]);
Thread t1=new Thread(tpo);
t1.start();
}
//thread untuk setfitur tanpa rmi
int ukuran=idW.length;
ProsesSetfitur pro=new ProsesSetfitur(idW);
Thread t2=new Thread(pro);
t2.start();
this is the code in class threadprepo :
public class ThreadPrepo implements Runnable{
String host,server,c,webtext;
int port,idweb;
DataDB db=new DataDB();
public ThreadPrepo(String webtext,String host,String server,int port,int idweb){
this.webtext=webtext;
this.host=host;
this.server=server;
this.port=port;
this.idweb=idweb;
}
#Override
public void run(){
preponi();
}
public synchronized void preponi(){
try{
System.out.println("hostnya :"+host);
Registry reg=LocateRegistry.getRegistry(host,port);
Sportrmijob rmi=(Sportrmijob) reg.lookup("server");
rmi.SetInput(webtext);
List l=rmi.getresult();
String[] hasilprep=new String[l.size()];
for(int k=0;k<l.size();k++){
hasilprep[k]=l.get(k).toString();
}
db.insertWordney(idweb, hasilprep);
String [][] frekdb=db.getFrekDB(idweb);
db.doinsertfrek(idweb,frekdb);
}
catch(Exception e){
System.out.println("error di class threadprepo "+e.getMessage());
}
}
}
and then this is code in class prosesSetFitur
public class ProsesSetfitur implements Runnable{
DataDB d=new DataDB();
int []idweb;
public ProsesSetfitur(int[]idweb){
this.idweb=idweb;
}
#Override
public void run(){
try{
Thread.sleep(500);
setfitur();
}
catch(Exception e){
System.out.println("error setfitur "+e.getMessage());
}
}
public synchronized void setfitur() throws InterruptedException{
System.out.println("(proses setfitur)");
String []allkata;
String fitur;
String []fiturs=new String[15];
String []kata_kata=new String[15];
System.out.println("nilai iD="+idweb.length);
for(int s=0;s<idweb.length;s++){
//System.out.println("IDWEEEEEEEEEEB"+idweb[s]);
allkata=d.getUrutanKata(idweb[s]);
for(int u=0;u<15;u++){
// System.out.println("PERULANGAN U KE"+u);
if(u<=4){
fitur="T";
//System.out.println("kata ke" +u+" = "+allkata[u]+" fiturnya = "+fitur);
kata_kata[u]=allkata[u];
fiturs[u]=fitur;
}
else if(u>4&&u<10){
fitur="S";
//System.out.println("kata ke"+u+" = "+allkata[u]+" fiturnya = "+fitur);
kata_kata[u]=allkata[u];
fiturs[u]=fitur;
}
else if(u>=10&&u<15){
fitur="R" ;
//System.out.println("kata ke"+u+" = "+allkata[u]+" fiturnya = "+fitur);
kata_kata[u]=allkata[u];
fiturs[u]=fitur;
}
}
d.insertfitur(idweb[s], kata_kata, fiturs);
}
}
can anyone give me solution to solve this problem...why thread in class ProsesSetFitur is execute first?how synchronization thread can work?please help...
public void run(){
try{
Thread.sleep(500);
setfitur();
}
run() method called only once when you start a thread. Again for different thread run() method will be different and call only once for the thread.
Also your preponi() and setfitur() called single time from run(). thats why you should not put synchronized modifier before preponi() and setfitur().
you should use synchronized when multiple thread access same resource or same function or same code block to make it thread safe.
Thread []tArray=new Thread[idW.length];
for(int j=0;j<idW.length;j++)
{
webtext = d.getWebText(idW[j]);
ThreadPrepo tpo =new ThreadPrepo(webtext, host[j%jumhost], "server", 1099,idW[j]);
tArray[j]=new Thread(tpo);
tArray[j].start();
tArray[j].join();
}
//thread untuk setfitur tanpa rmi
int ukuran=idW.length;
ProsesSetfitur pro=new ProsesSetfitur(idW);
Thread t2=new Thread(pro);
t2.start();
A thread sleep may be a pragmatic solution but it is not a guarantee for thread synchronization. To coordinate thread actions you should go for the basic wait/notify pattern whereas the threads uses conditions to perform certain actions. For a good introduction read this articles.
http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html
http://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example
I'm very new to java and found an exercise to work on basic thread synchronization. The problem is to print out 12345678910 10987654321 repeatedly until the program stops. Ten different threads should be used.
This is my code so far:
I am first working on trying to get just the first number (number one to work), but it keeps giving me an exception
public static void main(String[] args){
threadOne one = new threadOne();
one.start();
}
}
class updateNumber{
private int i;
synchronized void increase(int s){
this.i=s;
System.out.println(i);
}
}
class threadOne extends Thread {
private updateNumber grab;
public void run() {
try{
grab.increase(1);
}
catch(Exception e){
System.out.println("error in thread one");
}
}
}
I may be going about this the completely wrong way, but I've read a lot of documentation and am just thoroughly confused.
It looks like you didnt create a new instance of update
class threadOne extends Thread {
private updateNumber grab;
public void run() {
try{
grab.increase(1); // null pointer reference...<<<<<<<
}
catch(Exception e){
System.out.println("error in thread one");
}
}
}
// you need to alocate memory to updateNumber like this
//private updateNumber grab = new updateNumber();
class threadOne extends Thread {
private updateNumber grab = new updateNumber();
public void run() {
try{
grab.increase(1);
}
catch(Exception e){
System.out.println("error in thread one");
}
}
}
How Join method work in Thread. If write join method in run method then its going to deadloack. Just need to information why its happening.
Code Snipet:
public class ThreadSchuduling extends Thread{
static ThreadSchuduling threadObj3;
public ThreadSchuduling(){
System.out.println("Default Constructor");
}
public ThreadSchuduling(String name){
System.out.println("Parameter Constructor");
}
public void run(){
try{
threadObj3.join();
}catch(Exception e){
System.out.println("Error in RUN "+e);
}
System.out.println(Thread.currentThread().getName());
for(int i = 0; i < 10; i++){
System.out.println("Value is = "+i);
}
}
public static void main(String[] args) {
ThreadSchuduling threadObj1 = new ThreadSchuduling("Thread1");
ThreadSchuduling threadObj2 = new ThreadSchuduling("Thread2");
threadObj3 = new ThreadSchuduling("Thread3");
ThreadSchuduling threadObj4 = new ThreadSchuduling("Thread4");
threadObj1.start();
threadObj2.start();
threadObj3.start();
System.out.println("Thread 3 is started");
threadObj4.start();
try{
threadObj3.join();
}catch(Exception e){
System.out.println("Errpr "+e);
}
System.out.println("Main Method completed");
}
}
I just want to complete the thread3 before the thread1 and thread2
You haven't explained what threadObj3 is... is that a reference to the same thread? If so, it's understandable that it will deadlock - it's waiting until it's finished, which it won't do because it's waiting!
What are you actually trying to achieve?
OMG,threadObj3 is always waiting by itself,if you want to complete thread3 before thread1 and thread2, you can set the priority,or it's hard to make sure thread3's execution before other thread.