Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/yaftp/utils/Test.java


1    /**
2    *
3    * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package com.yaftp.utils;
21  
22  import java.util.* ;
23  
24  /**
25  
26    used for misc test
27  
28  */
29  
30  public class Test {
31  
32    private Vector _synchronizer = new Vector() ;
33  
34    public Test() {}
35  
36    private void grab1Synchronizer( String threadName )
37    {
38      System.out.println ( threadName + " grab1 before Grabbing " ) ;
39      synchronized ( _synchronizer )
40      {
41        System.out.println ( threadName + " grab1 has grabbed and sleeps... " ) ;
42        try {
43          Thread.currentThread().sleep(3000);
44        } catch ( InterruptedException e )
45        { System.out.println ("interrupted") ;  }
46        System.out.println ( threadName + " grab1 terminated" ) ;
47      }
48    }
49  
50    private void grab2Synchronizer( String threadName )
51    {
52      System.out.println ( threadName + " grab2 before Grabbing " ) ;
53      synchronized ( _synchronizer )
54      {
55        System.out.println ( threadName + " grab2 has grabbed and sleeps... " ) ;
56        try {
57          Thread.currentThread().sleep(1000);
58        } catch ( InterruptedException e )
59        { System.out.println ("interrupted") ;  }
60      }
61        System.out.println ( threadName + " grab2 terminated" ) ;
62    }
63  
64    class _GRABBER_
65    extends Thread
66    {
67       int _which ;
68       public _GRABBER_( String name , int which)
69       {
70         super(name) ;
71         _which = which ;
72       }
73  
74       public void run()
75       {
76         while ( true )
77         {
78           if ( _which == 1 )
79             grab1Synchronizer( this.getName() ) ;
80           else
81             grab2Synchronizer( this.getName() ) ;
82         }
83       }
84    }
85  
86    public void testSynchronized()
87    {
88       _GRABBER_ test1 = new _GRABBER_("FIRST", 1) ;
89       _GRABBER_ test2 = new _GRABBER_("SECOND",2) ;
90       test1.start() ;
91       test2.start() ;
92    }
93  
94    public static void main( String args[] )
95    {
96      System.out.println("Test Started") ;
97      Test test = new Test() ;
98      test.testSynchronized() ;
99    }
100 
101 }