Is that possible to fetch and lock with java class

hi,
i want to know if it’s possible to fetch and lock an service task who using a java class and if someone have an example
thank you