Problem with Synchronized block

Hi,

For me synchronized key word is not working (?) :-(

Here is the example :

I have three classes

package sample.test;

public class TestThreadMain {

public static void main(String[] args) {

Thread t1 = new ThreadOne();

Thread t3 = new ThreadOne();

t1.start();

t3.start();

}

}

public class TestThread {

int k = 0;

public synchronized void method1(){

System.out.println("Entered into method1 >>>>>>>>>");

for (int i=0; i<1000; i++){

for (int j=0; j<100000; j++){

k = j * 12345;

}

System.out.println("method1 i value is "+i);

}

}

}

package sample.test;

public class ThreadOne extends Thread {

TestThread t1 = new TestThread();

public void run() {

t1.method1();

}

}

If I run "TestThreadMain" class output is

Entered into method1 >>>>>>>>>

method1 i value is 0

method1 i value is 1

method1 i value is 2

method1 i value is 3

method1 i value is 4

method1 i value is 5

method1 i value is 6

method1 i value is 7

method1 i value is 8

method1 i value is 9

method1 i value is 10

method1 i value is 11

method1 i value is 12

method1 i value is 13

method1 i value is 14

method1 i value is 15

method1 i value is 16

method1 i value is 17

method1 i value is 18

method1 i value is 19

method1 i value is 20

method1 i value is 21

method1 i value is 22

method1 i value is 23

method1 i value is 24

method1 i value is 25

method1 i value is 26

method1 i value is 27

method1 i value is 28

method1 i value is 29

method1 i value is 30

method1 i value is 31

method1 i value is 32

method1 i value is 33

method1 i value is 34

method1 i value is 35

method1 i value is 36

method1 i value is 37

method1 i value is 38

method1 i value is 39

method1 i value is 40

method1 i value is 41

method1 i value is 42

method1 i value is 43

method1 i value is 44

method1 i value is 45

method1 i value is 46

method1 i value is 47

method1 i value is 48

method1 i value is 49

method1 i value is 50

method1 i value is 51

method1 i value is 52

method1 i value is 53

method1 i value is 54

method1 i value is 55

method1 i value is 56

method1 i value is 57

method1 i value is 58

method1 i value is 59

method1 i value is 60

method1 i value is 61

method1 i value is 62

method1 i value is 63

method1 i value is 64

method1 i value is 65

method1 i value is 66

method1 i value is 67

method1 i value is 68

method1 i value is 69

method1 i value is 70

method1 i value is 71

method1 i value is 72

method1 i value is 73

method1 i value is 74

method1 i value is 75

method1 i value is 76

method1 i value is 77

method1 i value is 78

method1 i value is 79

method1 i value is 80

method1 i value is 81

method1 i value is 82

method1 i value is 83

method1 i value is 84

method1 i value is 85

method1 i value is 86

method1 i value is 87

method1 i value is 88

method1 i value is 89

method1 i value is 90

method1 i value is 91

method1 i value is 92

method1 i value is 93

method1 i value is 94

method1 i value is 95

method1 i value is 96

method1 i value is 97

method1 i value is 98

method1 i value is 99

method1 i value is 100

method1 i value is 101

method1 i value is 102

method1 i value is 103

method1 i value is 104

method1 i value is 105

method1 i value is 106

method1 i value is 107

method1 i value is 108

method1 i value is 109

method1 i value is 110

method1 i value is 111

method1 i value is 112

method1 i value is 113

method1 i value is 114

method1 i value is 115

method1 i value is 116

method1 i value is 117

method1 i value is 118

method1 i value is 119

method1 i value is 120

method1 i value is 121

method1 i value is 122

method1 i value is 123

method1 i value is 124

method1 i value is 125

method1 i value is 126

method1 i value is 127

method1 i value is 128

method1 i value is 129

method1 i value is 130

method1 i value is 131

method1 i value is 132

method1 i value is 133

method1 i value is 134

method1 i value is 135

method1 i value is 136

method1 i value is 137

method1 i value is 138

method1 i value is 139

method1 i value is 140

method1 i value is 141

method1 i value is 142

method1 i value is 143

method1 i value is 144

method1 i value is 145

method1 i value is 146

method1 i value is 147

method1 i value is 148

method1 i value is 149

method1 i value is 150

method1 i value is 151

method1 i value is 152

method1 i value is 153

method1 i value is 154

method1 i value is 155

method1 i value is 156

method1 i value is 157

method1 i value is 158

method1 i value is 159

method1 i value is 160

method1 i value is 161

method1 i value is 162

method1 i value is 163

method1 i value is 164

method1 i value is 165

method1 i value is 166

method1 i value is 167

method1 i value is 168

method1 i value is 169

method1 i value is 170

method1 i value is 171

method1 i value is 172

method1 i value is 173

method1 i value is 174

method1 i value is 175

method1 i value is 176

method1 i value is 177

method1 i value is 178

method1 i value is 179

method1 i value is 180

method1 i value is 181

method1 i value is 182

method1 i value is 183

method1 i value is 184

method1 i value is 185

method1 i value is 186

method1 i value is 187

method1 i value is 188

method1 i value is 189

method1 i value is 190

method1 i value is 191

method1 i value is 192

method1 i value is 193

method1 i value is 194

method1 i value is 195

method1 i value is 196

method1 i value is 197

method1 i value is 198

method1 i value is 199

method1 i value is 200

method1 i value is 201

method1 i value is 202

Entered into method1 >>>>>>>>>

method1 i value is 0

method1 i value is 1

method1 i value is 2

method1 i value is 3

method1 i value is 4

method1 i value is 5

method1 i value is 6

method1 i value is 7

method1 i value is 8

method1 i value is 9

method1 i value is 10

method1 i value is 11

method1 i value is 12

method1 i value is 13

method1 i value is 14

method1 i value is 15

method1 i value is 16

method1 i value is 17

method1 i value is 18

method1 i value is 19

method1 i value is 20

method1 i value is 21

method1 i value is 22

method1 i value is 23

method1 i value is 24

method1 i value is 25

method1 i value is 26

method1 i value is 27

method1 i value is 28

method1 i value is 29

method1 i value is 30

method1 i value is 31

method1 i value is 32 .....

My question is as i delcared the method " method1()" as synchronized, after completion of printing the first 1000 only second thread has to enter in to this method.

Am I wrong any where ?

Thanks,

Sudhakar

[7992 byte] By [sudhavasirajua] at [2007-11-27 3:45:44]
# 1
1. Use code tags when you post code.2. Console output isn't going to tell you how threads work.
jschella at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 2

1) When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

2) Was it really necessary to post 200 lines or so of output? Couldn't you have posted just a few to give us the general idea?

3) It's not clear to me what your question is. Are you saying you expected something like

T1

T2

T1

T2

etc.

?

If so, then you're mistaken. The whole point of multithreading is that you don't know or care which thread executes which instructions when. There's no "execute a statement then switch threads" rule. That would occur ridiculous amounts of overhead.

jverda at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 3

> 1) When you post code, please use[code] and

> [/code] tags as described in

> [url=http://forum.java.sun.com/help.jspa?sec=formattin

> g]Formatting tips[/url] on the message entry

> page. It makes it much easier to read.

>

Sure. I will do it from next time.

> 2) Was it really necessary to post 200 lines or so of

> output? Couldn't you have posted just a few to give

> us the general idea?

I think it will give better understanding. So ...

> 3) It's not clear to me what your question is. Are

> you saying you expected something like

>

> T1

> T2

> T1

> T2

> etc.

>

> ?

>

> If so, then you're mistaken. The whole point of

> multithreading is that you don't know or care which

> thread executes which instructions when. There's no

> "execute a statement then switch threads" rule. That

> would occur ridiculous amounts of overhead.

As per my understanding, If one thread enters into synchronized method, the other threads needs to wait until the completion of first one.

So, first it needs to print

method1 i value is 1

method1 i value is 2

...

method1 i value is 1000

then second thread should start printing.

Is my understanding wrong or ... ?

sudhavasirajua at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 4

> > 2) Was it really necessary to post 200 lines or so

> of

> > output? Couldn't you have posted just a few to

> give

> > us the general idea?

>

> I think it will give better understanding. So ...

>

Okay, look:

1

2

3

4

...

200

Conveys the same information with much less clutter.

> As per my understanding, If one thread enters into

> synchronized method, the other threads needs to wait

> until the completion of first one.

Yes. But tha doesn't mean that when T1 finishes the sync block, T2 will be next.

T1 finishes sync block. Now T1 and T2 (and maybe T3...T100) are waiting to enter that block. *ANY* thread can enter it--it's up to the scheduler, which is partly in the hands of the JVM and partly in the hands of the OS. But regardless of whether there's a sync block or not, the scheduler isn't generally going to swap threads that often--it's just wasteful.

The sync block means other threads MUST wait. It doesn't guarantee that when it's done, some other thread will execute.

jverda at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 5
Your two threads are not synchronizing on the same object -- each thread creates its own local object. So the synchonization is doing nothing useful in your case.
DrLaszloJamfa at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 6

> Yes. But tha doesn't mean that when T1 finishes the

> sync block, T2 will be next.

>

> T1 finishes sync block. Now T1 and T2 (and maybe

> T3...T100) are waiting to enter that block. *ANY*

> thread can enter it--it's up to the scheduler, which

> is partly in the hands of the JVM and partly in the

> hands of the OS. But regardless of whether there's a

> sync block or not, the scheduler isn't generally

> going to swap threads that often--it's just

> wasteful.

>

> The sync block means other threads MUST wait. It

> doesn't guarantee that when it's done, some other

> thread will execute.

First Thread has to complete its execution, right. Then it has to print all the values(upto 1000). But after part of execution(printing about 200), how second thread entered in to that method and started printing?

sudhavasirajua at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 7
> First Thread has to complete its execution, right. Then it has to print all the > values(upto 1000). But after part of execution(printing about 200), how second > thread entered in to that method and started printing?See reply #5
DrLaszloJamfa at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 8

> Your two threads are not synchronizing on the

> same object -- each thread creates its own local

> object. So the synchonization is doing nothing useful

> in your case.

OK. Then if I want to write one synchronized method in some class, (I am not aware of threads which are going to execute this method) how should i do that ?

sudhavasirajua at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 9

> Your two threads are not synchronizing on the

> same object -- each thread creates its own local

> object. So the synchonization is doing nothing useful

> in your case.

Nice catch.

I didn't even bother trying to read that unformatted code. I assumed the question was "why don't they alternate printing one line each?"

jverda at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 10

> > Your two threads are not synchronizing on

> the

> > same object -- each thread creates its own local

> > object. So the synchonization is doing nothing

> useful

> > in your case.

>

> OK. Then if I want to write one synchronized method

> in some class, (I am not aware of threads which are

> going to execute this method) how should i do that ?

If you want all those threads to wait for each other, they all have to syncrhonize on the same object.

jverda at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 11

Demo:

public class ThreadyGoodness implements Runnable {

private Object lock;

public ThreadyGoodness(Object lock) {

this.lock = lock;

}

public void run() {

for(int i=0; i < 10; ++i) {

synchronized(lock) {

for (int j=0; j < 10; ++j) {

pause();

System.out.format("Thead %s i=%d j=%d%n", Thread.currentThread().getName(), i, j);

}

}

}

}

private void pause() {

try {Thread.sleep(100);} catch (InterruptedException e){}

}

public static void main(String[] args) {

Object lock = new Object();

new Thread(new ThreadyGoodness(lock), "thread 1").start();

new Thread(new ThreadyGoodness(lock), "thread 2").start();

}

}

The synchronization here guarantees that the j loop is run atomically.

DrLaszloJamfa at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...
# 12
My example is full of thready goodness :-)
DrLaszloJamfa at 2007-7-12 8:49:28 > top of Java-index,Java Essentials,Java Programming...