It is good for me that I have been afflicted. Whatever may have been the form of affliction, it was good for me. The design was benevolent; the result has been my own benefit. This will be the experience sooner or later resulting from all the afflictions of the righteous.
-Barnes Notes on the Bible for Psalm 119:71
To paraphrase, any affliction of the righteous will be worked out into benefit, into joy!!
And so, we can thank God for our afflictions! And when we are able to thank God for our afflictions, we sink deeper into the true meaning of JOY.
I will extol the Lord at all times; His praise will always be on my lips.
In accordance, Lord God, I thank You for all of my afflictions. I thank You for times of darkness and of hurting. I thank You because You have worked (and continue working) all these things out for my benefit, Your glory. You are a gracious, loving God. Even in the midst of trial and judgment, I will trust You when You say You are perfectly loving and caring. You are worthy of all our praise.