The growing threat of Somali pirates has led to a growing industry supplying technology and services for stopping the pirates from getting aboard. One example is an electric fence that can be set up along the edge of the deck. Once turned on, it will deliver a disabling, 9,000 volt, jolt to anyone touching it. Some ships already string barbed wire around likely boarding areas. Crews also practice 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.
Another tech item is a water cannon, a more powerful version of the fire hoses that are often used to keep the pirates from boarding. Some ships simply carry a longer nozzle for their fire hoses, which creates a more powerful, and concentrated, stream of water. A more expensive solution (several thousand dollars) is a sonic cannon (that directs a beam of very loud sound at someone hundreds of meters away), which works in most cases. Some crews have a larger supply of maritime flare guns. These can be fired right at the pirate boat, and will do lots of damage, because these industrial strength flares use magnesium, which not only burns very bright, but also very, very hot. As in hot enough to burn a hole through the bottom of the pirate boat. You cannot extinguish magnesium with water.
However, 80 percent of the attacks defeated do not involve any need to battle pirates trying to board. 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 (more economical and safer) 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.
Many crews are trained to 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.