I've seen the pilots use the restroom during the flight several times while flying F. Obviously the cockpit door has to be opened during this time and I've been asked by the purser to wait for a few seconds so that the door could be closed. I was told that due to security issues the crew have to ensure that there are no pax around near the cockpit door when it is opened.