Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java JavaSE6
I create two streams in the different ways. Each stream in own way works from a variable. Question - why they work not serially, т.е the first stream the first began, the first and has to finish, and it turns out differently
 
package Package;
 
public class Resurse
{
   int i=0;
   boolean valueSet=false;
   
void call()
  {     
      System.out.println(i);     
  }
}
 
 
package Package;
 
public class NewThread_Runnable implements Runnable
{
Resurse s;
 
    NewThread_Runnable(Resurse s)
    {
        this.s=s;
        new Thread(this,"Первый поток").start();    
    }
    public void run()
    {
        try
        {
            for(int i=100;i>0;i--)
            {
                System.out.println("Первый поток " + i);
                  
                synchronized(s)
                { 
                    s.i+=10;
                    s.call();
                }                   
                Thread.sleep(10);
            }
        }
        catch (InterruptedException e)
        {
            System.out.println("Первый поток прерван.");
        }
        System.out.println("Первый поток завершен.");
    }
}
 
package Package;
 
public class NewThread_thread extends Thread
{   
  Resurse s;    
    
    NewThread_thread (Resurse s)
    {
    //  super();
    //  System.out.println("Поток,созд. от класса Thread "+ this);
        this.s=s;       
        new Thread(this," Второй поток ").start();
        
    }
      public void run()
    {
        try
        {
            for(int i=100;i>0;i--)
            {               
                System.out.println("Второй поток " + i);
          
                synchronized(s)
                {
                s.i+=50;
                s.call();                   
                }               
                Thread.sleep(10);
                
            }
        }
        catch (InterruptedException e)
        {
            System.out.println("Второй поток прерван.");
        }
        System.out.println("Второй поток завершен.");
    }
}
 
 
package Package;
 
public class Program {
 
    public static void main(String[] args)  
    {   
    Resurse s1=new Resurse();           
     new NewThread_Runnable(s1);
     new NewThread_thread(s1);
    
/*   try{
        ob.join();
        ob2.join();     
       }catch(InterruptedException e)
       {
           System.out.println("прервано");
       }
    */
  
    }
}
 
Posted 27-Mar-13 8:04am
Comments
Sergey Alexandrovich Kryukov at 27-Mar-13 15:34pm
   
There is nothing in your code which would synchronize those operations, nothing at all. It's weird why are you asking about it. And why using threads if you want to defeat parallel execution nearly completely.
I hope your code is purely experimental, as it's no good for any working purposes.
I would also advise to use English only, especially when you are posting in international forum.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100