Hello to all
first look this code

class Square1 implements Runnable{

    int a;
    public void run(){

class Square2 implements Runnable{

    Square1 ob=new Square1();
    public void run(){

        System.out.println("Square Of this number is = "+(ob.a*ob.a));
class SquareThread{

    public static void main(String args[]){

        Thread t1=new Thread(new Square1());
        Thread t2=new Thread(new Square2());

        }catch(InterruptedException e){


i need to find the square but answer is 0 so tell me how to find the square

You need to run thread1 inside thread2 and create a mutex that thread1 will lock and set the data value, in the meantime, thread2 will wait on the mutex and when thread1 releases it and thread2 gets the lock, it can multiply the value (t1.a) by itself and then print the value. Note that I am not writing your code for you... :-)

So, see what you can do with this and then we will critique your code, other than to note that there is no synchronization in your code, which is why my comment is trying to show you what to do to fix that.

... also, you have two instances of Square1 (lines 11 and 21). You try to use the value from the first instance, but you only execute the run method for the second instance

... and, line 29, a printStackTrace() will show you the stack. getStackTrace() returns the stack, but you don't do anything with it!

I try my best but i do not understand it plz help me and write its correct code that calculate the square. i am waiting.

I am afraid you will wait indefinitely if you expect us to do the work for you. While we are here to help you learn, doing your work for you doesn't actually accomplish that. As a matter of policy for both Daniweb in general and most members in particular, we do not provide homework solutions whole cloth. We will help you solve problems, and explain why things may not be working as you want them to, but we won't write your code for you.

then tell me how can i solve my problem I join this forum for good knowledge about programming but you are not cooprative I share my problem with all of you so please help me i try my best to solve this problem

I realize that we seem to be dragging our feet, but the thing is, we are trying to avoid the many people who are trying to get free homework solutions. We get such things all too often, and as a result, we generally don't geive direct solutions, only advice.

In this case, there are a few things that stand out. First, it is hard to see where synchronized would come up in this, since there aren't any shared values anywhere. I realize you are trying to understand thread synchronization better, but this example doesn't seem to need it.

Second, the way you currently have things, the first thread is completing before the second thread is begun (indeed, given that it is just a single assignment, it probably completes before you .join it), which, as I understand it, has the effect that the instance variable is no longer in scope even if you access it from the thread object. Any corrections on this would be appreciated, BTW.

Third, as JamesCherill pointed out, you want to use e.printStackTrace() rather than e.getStackTrace() on line 29, in order to see the results of the exception, if one is raised. As it is, it fetches the stack trace but does nothing with it.

If this is an assignment, could you please post the exact wording of the project? That will help us get an idea of what you need. Ordinarily, we wouldn't ask for that, but the program requirements aren't very clear as to why, for example, this has to be done with threading at all.

As a bit of speculation, was the assignment by any chance to raise the value to any power, not just squaring? That would explain the multi-threading, as the solution could involve spawning threads to break the problem down by halves. If this is the case, the general solution would involve something like this:

main thread:
    get n to square as an int
    get expt as an int
    Integer result = 0
    spawn a pow thread with (n, expt, result) as its arguments

pow thread (n, expt, rsult):
    synchronized (result):
        if expt is 0:
            result += 1
        else if expt is 1:
            result += n
        else if expt is even:
            spawn two pow threads (n, (expt / 2))
            result = result * result
            spawn a pow thread (n, (expt - 1))
            result = n * result

This is just a quick sketch; I haven't tried it out. But it should at least give you a place to start.

i am new in java and also new in multithreading. This is not my assignment it is only my own practice with this practice i want to understand multithreading and i want to write a program in which we create two threads, first thread get a number from the user or set the value in program and second thread show its square
so if you give me the code then thank you very much

i need a correct code that show the square

if u really want to learn then think harder and try even harder, u will get what are u looking for..when i was learning java, these kind of problems also occured to me and sometimes it was better to solve them by themselves rather than copying anyone else'e code!!!

i need a correct code that show the square

No you don't. You need to learn to write it yourself.

You have had a massive amount of help from DaniWeb but patience is wearing thin. If you don't start to work and think harder for yourself then you can't expect much more spoon-feeding.

just tell me will this task done by synchronization???????

Synchronization has absolutely nothing to do with calculating the square of a number.

is this can perform without synchronization??????

do you know even the tiniest sliver of primary school math?
Like how to multiply two numbers for example?

i know about multiplication but problem is that input is in seperate thread and Square will be calculate in separate thread and print output