The thymus gland, despite containing glandular tissue and producing several hormones, is much more closely associated with the immune system than with the endocrine system. The thymus serves a vital role in the training and development of T-lymphocytes or T cells, an extremely important type of white blood cell. T cells defend the body from potentially deadly pathogens such as bacteria, viruses, and fungi.
The thymus is a soft, roughly triangular organ located in the mediastinum of the thoracic cavity anterior and superior to the heart and posterior to the sternum. It has two distinct but identical lobes that are each surrounded by a tough, fibrous capsule. Within each lobe is a superficial region of tissue called the cortex and a histologically distinct deep region called the medulla. Epithelial tissues and lymphatic tissues containing dendritic cells and macrophages make up the majority of both regions of the thymus.
The function of the thymus is to receive immature T cells that are produced in the red bone marrow and train them into functional, mature T cells that attack only foreign cells. T cells first reside within the cortex of the thymus where they come in contact with epithelial cells presenting various antigens. The immature T cells that respond to the antigens corresponding to foreign cells are selected to survive, mature, and migrate to the medulla while the rest die via apoptosis and are cleaned up by macrophages. This process is known as positive selection.
Upon reaching the medulla, the surviving T cells continue to mature and are presented with the body's own antigens. T cells that bind to the body's own antigens test positively for autoimmunity, whereby they attack the body's own cells instead of only foreign cells. Autoimmune T cells are eliminated by apoptosis in a process known as negative selection, resulting in only around 2% of the immature T cells reaching maturity.
Several hormones produced by the thymus promote the maturation of the T cells prior to their release into the bloodstream. The now mature T cells circulate through the body where they recognize and kill pathogens, activate B cells to produce antibodies, and store the memory of past infections.
Unlike most organs that grow until the age of maturity, the thymus enlarges throughout childhood but slowly shrinks from the onset of puberty and throughout adulthood. As the thymus shrinks, its tissues are replaced by adipose tissue. The shrinking is due to the reduced role of the thymus in adulthood --- the immune system produces most of its T cells during childhood and requires very few new T cells after puberty.