Click here to Skip to main content
15,887,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

Java
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
Comments
Sergey Alexandrovich Kryukov 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900