CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Jakhongir Ruziev
Level 23
Tashkent
  • 22.04.2020
  • 934views
  • 3comments

I get this: "All of the Solution class's methods, except for the main method, must be synchronized." Even if I did so. What am I missing?

Question about the task Shared list
Java Core,  Level 7,  Lesson 10
Resolved

1. Change the Solution class so that it becomes a list. (It must implement the java.util.List interface).
2. The Solution list should only work with Longs.
3. Use the original field.
4. The list will be used by threads, so you need to ensure that all the methods are synchronized.

Requirements:
  • The Solution class must implement the List<Long> interface.
  • The Solution class must have a private ArrayList<Long> field called original.
  • All of the List interface's overridden methods must delegate calls to the methods of the original object.
  • All of the Solution class's methods, except for the main method, must be synchronized
package com.codegym.task.task17.task1713; import java.util.*; /* Shared list */ public class Solution implements List<Long>{ private ArrayList<Long> original = new ArrayList<>(); public static void main(String[] args) { } @Override public synchronized int size() { return original.size(); } @Override public synchronized boolean isEmpty() { return original.isEmpty(); } @Override public synchronized boolean contains(Object o) { return original.contains(o); } @Override public synchronized Iterator<Long> iterator() { return original.iterator(); } @Override public synchronized Object[] toArray() { return original.toArray(); } @Override public synchronized <T> T[] toArray(T[] a) { return (T[]) original.toArray(a); } @Override public synchronized boolean add(Long aLong) { return original.add(aLong); } @Override public synchronized boolean remove(Object o) { return original.remove(o); } @Override public synchronized boolean containsAll(Collection<?> c) { return original.containsAll(c); } @Override public synchronized boolean addAll(Collection<? extends Long> c) { return original.addAll(c); } @Override public synchronized boolean addAll(int index, Collection<? extends Long> c) { return original.addAll(index, c); } @Override public synchronized boolean removeAll(Collection<?> c) { return original.removeAll(c); } @Override public synchronized boolean retainAll(Collection<?> c) { return original.retainAll(c); } @Override public void clear() { original.clear(); } @Override public synchronized Long get(int index) { return original.get(index); } @Override public synchronized Long set(int index, Long element) { return original.set(index, element); } @Override public synchronized void add(int index, Long element) { original.add(index, element); } @Override public synchronized Long remove(int index) { return original.remove(index); } @Override public synchronized int indexOf(Object o) { return original.indexOf(o); } @Override public synchronized int lastIndexOf(Object o) { return original.lastIndexOf(o); } @Override public synchronized ListIterator<Long> listIterator() { return original.listIterator(); } @Override public synchronized ListIterator<Long> listIterator(int index) { return original.listIterator(index); } @Override public synchronized List<Long> subList(int fromIndex, int toIndex) { return original.subList(fromIndex, toIndex); } }
+2
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
jajaceek
Level 23 , Warszawa, Poland
22 April 2020, 10:25solution
look at line 83
+2
Jakhongir Ruziev QA Automation Engineer at Exadel
22 April 2020, 11:21
Oh yes, Thank you! 😊
0
Monisha
Level 19 , Hyderabad, India
12 June 2020, 09:43
Hey ....please say da correction please... After all da attempts I m tired....
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.