Off Somalia, 136 ships have been attacked so far this year, and 28 (21 percent) have been taken. Last year, the pirate success rate was 40 percent. Moreover, 80 percent of the attacks defeated do not involve the foreign warships now patrolling the coast. The merchant sailors, and the ship owners, have adopted defensive measures that have become remarkably successful in defeating pirate attacks. For the captain himself, the best defense is knowing what speeds and maneuvers his ship can use to keep the pirates away. Larger ships can create dangerous wakes for the pursuing speedboats, by zig zagging. Captains also have to learn how fast their ship can accelerate to escape oncoming speed boats. Normally, captains are more skilled at moving their ships at slower speeds. Putting the pedal to the metal and hot roding around the high seas is not normally part of their skill set. But that's how you avoid getting hijacked by pirates. Captains are learning, and so are their crews.
Ship captains are organizing and drilling their crews on things that can be done to keep the pirates from getting aboard. This ranges from stringing barbed wire around likely boarding points, to practicing the use of fire hoses and other tools (like long poles) to keep the ladders or grappling hooks from enabling the pirates to get aboard. These drills build confidence in the ability of the crew to defend their ship.
The sailors also now keep track of where the nearest warships are, and prepare a "safe room" (an area of the ship the crew can barricade themselves in, until help arrives.) This includes providing emergency communications in the safe room. All this takes advantage of the fact that the pirates cannot take control of the ship unless they have the crew. Usually this comes down to barricading the crew in the engine compartment. If the crew prepares for that eventuality (having a radio available to contact the warships, along with water, food and medical supplies there), just getting everyone into the engine room when it appears that the pirates are going to get on board, means that the pirates will be caught between the crew they cannot reach, and the approaching warship that can certainly reach the pirates.