Q:

how to make a vector synchronized such that from two threads only one thread will access it

In My Program there are two classes A and B, both of them extend Thread, Now B has a Queue named CommandQueue. Now Thread B access it until it becomes empty and Class A thread access it whenever found new commands to insert in it. How can I make this queue synchronized such that at a time both thread can not access it, and when one has used this queue, make it free to be used by another thread.  Also please suggest if there will not be any problem in case I do not make it synchronized.

A:

Seems you need a BlockingQueue instead of plain Vector.
BlockingQueue.put(E e): Inserts the specified element into this queue, waiting if necessary for space to become available. 
BlockingQueue.take(): Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. 

A:

Whenever you want to use Threads and queues together to perform work, it is highly likely that an ExecutorService is the best choice.  This is because they are designed to wrap up both of these concepts.
ExecutorService es = Executors.new (pick a thread pool of your choice)

// in thread A
es.execute(new Runnable() {
   public void run() {
      // task to run on Thread B
   }
});

// in thread A
Future<Type> future = es.submit(new Callable<Type>() {
   public Type call() {
      // task to run on Thread B and ...
      return type;
   }
});

// later get the result
Type t = future.get();

Note: you don't have to refer to the queue or thread pool directly, all you need is to say what you want it to do.

