[VV23] Java synchronized, ResponseBody, React spinner, TDD benefits, chatGPT regex, engaging conversation, preferred IDE
???JAVA CERTIFICATION QUESTION: Multithreading and the Java keyword synchronized
??The goal is to obtain consistent results and avoid unwanted effects.
Imagine that you are working with multiple instances of the following SyncMe class, and the instances are used by multiple Java threads:
public class SyncMe {
??protected static synchronized void hi() {
????System.out.print("hi ");
????System.out.print("there! ");
??}
??public synchronized void bye() {
????System.out.print("bye ");
????System.out.print("there! ");
??}
??public synchronized void meet() {
????hi();
????bye();
??}
}
What statements are true about the class? Choose two.
- Concurrent calls to the hi() methods can sometimes print hi hi.
- Concurrent calls to the bye() methods can sometimes print bye bye.
- Concurrent calls to the meet() method always print hi there! bye there!.
- Concurrent calls to the meet() method can print bye bye.
- Concurrent calls to the meet() method can print hi hi.
Answer:?"concurrent calls to the bye() methods can sometimes print bye bye." and? "concurrent calls to the meet() method can print bye bye."
Given this discussion and noting that the hi() method is static but the other two are instance methods,?and also that the question states that multiple objects exist,?recognize that only one thread at a time can be executing the hi() method,? but more than one thread might be executing the other two methods.
That tells you that whenever hi has been printed, another hi cannot be printed until after the printing of there!
Using the same logic as above, concurrent calls to meet() cannot result in hi hi being printed either.
By contrast, concurrent calls to the bye() method can execute concurrently if they are invoked on different instances of the class.?
In such a situation the output of the two invocations can become interleaved, and you might in fact see bye bye printed.?
?
???? SPRING CERTIFICATION QUESTION:?When do you need @ResponseBody?
Answer:
???????????????????????? annotation is a step further from a classic MVC approach to a REST approach when ???????????????????????? is not needed and request-handler result might be directly written to the http-response body.
???????????????????????? annotation does exactly that, enabling functionality offered by ???????????????????????????????????????? message-converters.
Note: ???????????????????????? annotation might be applied at a type level,? thus all handler methods declared within that type will inherit declared behavior.
The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.
Suppose we have a custom Response object:
???????????? ?????????? ???????????????????????????????? {
??????????????? ???????????? ????????;?
?// ???????????????? ??????????????/??????????????
}
Next, the associated controller can be implemented:
@??????????????????r
@????????????????????????????("/????????")
???????????? ?????????? ?????????????????????????????????????????? {
@??????????????????
????????????????????????????? ????????????????????????????;
@??????????????????????("/????????????????")
?@????????????????????????
????????????? ???????????????????????????????? ????????????????????????????????????????????(
??@?????????????????????? ?????????????????? ??????????????????) {
? ????????????? ?????? ????????????????????????????????("???????????? ?????? ??????????????!!!");
??}
}
In the developer console of our browser or using a tool like Postman, we can see the following response:
{"????????":"???????????? ?????? ??????????????!!!"}
Remember, we don't need to annotate the @RestController-annotated controllers with the Next ResponseBody annotation since Spring does it by default.
@ResponseBody ???https://lnkd.in/eQPfu244
?
QUOTE???
?
REACT: Create a loading Spinner in React
To create a loading spinner in React, you can use a library such as "react-spinners" or "react-loader-spinner", which provide pre-built loading spinner components that you can easily import and use in your application.
Alternatively, you can create your own custom loading spinner using CSS and JavaScript. Here's an example of a basic loading spinner implemented using CSS only:
React Loading Spinner component ??
领英推è
import React, { useState } from 'react';
function LoadingSpinner() {
? const [loading, setLoading] = useState(true);
? return (
? ? <div className={`loading-spinner ${loading ? 'visible' : 'hidden'}`}>
? ? ? <div className="spinner"></div>
? ? </div>
? );
}
export default LoadingSpinner;
CSS style sheet ??
/* CSS */
.loading-spinner {
? position: fixed;
? top: 0;
? left: 0;
? width: 100%;
? height: 100%;
? background-color: rgba(255, 255, 255, 0.5);
? z-index: 999;
? display: flex;
? justify-content: center;
? align-items: center;
}
.loading-spinner.hidden {
? display: none;
}
.loading-spinner .spinner {
? border: 4px solid #f3f3f3;
? border-top: 4px solid #3498db;
? border-radius: 50%;
? width: 40px;
? height: 40px;
? animation: spin 0.8s linear infinite;
}
@keyframes spin {
? 0% { transform: rotate(0deg); }
? 100% { transform: rotate(360deg); }
}
You can 'useState' to toggle the visibility of the loading spinner.
?
SCRUM DEVELOPER CONCEPT: Benefits of Test-Driven Development ??
1) It promotes good design?? and separation of concerns.
2) It improves quality and reduces bugs ??.
3) It leads to the construction ?? ♂? of an automated test harness.
4) It speeds up ?? ♂? the overall development process.
5) it reduces ?? the cost of maintenance over time.
?
chatGPT: Regex with chatGPT ??
I asked chatGPT??
Generate a SprinBoot JUnit test asserting a regex that contains only letters, or dashes, or dots, or spaces?
It answered??
import org.junit.Assert;
import org.junit.Test;
public class RegexTest {
private static final String regex = "[a-zA-Z\\-\\.\\s]*";
@Test
public void testRegex() {
Assert.assertTrue("abc-def.ghi ijk ".matches(regex));
Assert.assertTrue("abcdefghijk".matches(regex));
Assert.assertTrue("".matches(regex));
Assert.assertTrue("vinny using chaGPT -- Awesome.".matches(regex));
Assert.assertFalse("abc-def.ghi ijk123".matches(regex));
}
}
?
SOFT SKILL: Engaging Conversation ??
Having engaging conversations that connect with your interlocutor involves several key strategies:
- Active listening: Pay close attention to what the other person is saying and respond in a way that shows you understand and value their perspective. Avoid interrupting and instead, paraphrase what they've said to confirm your understanding.
- Showing interest: Ask open-ended questions and express genuine interest in the other person's thoughts, feelings, and experiences. Encourage them to share more about themselves by following up on their statements.
- Building rapport: Find common ground by sharing your own thoughts and experiences that are relevant to the conversation. Show empathy and understanding by acknowledging their feelings and perspectives.
- Being present: Avoid distractions and give the conversation your full attention. Avoid multitasking and instead, focus on the present moment and the person you are talking to.
- Being authentic: Be honest and transparent in your communication. Share your own thoughts and feelings in an appropriate and respectful way.
- Being flexible: Be open to changing the topic or direction of the conversation if it seems appropriate. Be willing to adjust your communication style to suit the needs and preferences of the person you are talking to.
Ultimately, connecting with others through conversation is about building trust and understanding through active listening, showing interest, building rapport, being present, being authentic, and being flexible.
?
POLL???: Which IDE do you use for Java programming? VS/ IJ/ Eclipse/ Other
?