홈>
메인 클래스를 실행할 때 예외가 발생합니다 : 스레드 예외
실행 방법을 사용하며 다음과 같습니다.
@Override
public synchronized void run() {
// TODO Auto-generated method stub
int randomNum = ThreadLocalRandom.current().nextInt(1, 4);
Student studentThread = new Student();
Inlay inlayThread = new Inlay();
studentThread.setId(counter);
inlayThread.setInlayId(randomNum);
Course courseThread = new Course();
courseThread.setInlayD(inlayThread);
courseThread.setStudentD(studentThread);
this.arrCourse.add(courseThread);
System.out.println(courseThread.toString());
counter++;
}
그리고 나의 주 :
Course c = new Course();
for(int i = 0 ; i<200;i++)
{
Thread t = new Thread(c);
t.start();
}
c.toString();
Tihs 예외는 무엇입니까?
감사합니다!
-
답변 # 1
-
답변 # 2
공통 구현
ArrayList
에 항목을 추가하려고합니다. 또는List
스레드 안전하지 않으며ConcurrentModificationException
를 유발합니다. 최적화되지 않은 구조를 수정 (추가/삭제) 할 때 발생합니다.Collections.synchronizedList
를 사용하는 것이 좋습니다 포장지 :Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list.
List<Course> arrCourse = Collections.synchronizedList(new ArrayList<>());
관련 자료
- java - 콜 러블을 사용할 때 메인 스레드는 어떻게 되나요?
- spring - nativeJPQL 삭제 쿼리 사용 중 예외
- angular - ngClass 지시문을 사용하면 예외가 발생합니까?
- python - Tkinter를 사용할 때 스레드에서 루프 중지
- apache poi - 스레드 "main"의 예외 javalangillegalargumentexception - 허용 범위 (01048575)를 벗어난 잘못된 행 번호 (-1)
- windows - flutter doctor --android-licenses - 스레드 "main"javalangnoclassdeffounderror의 예외
- json - 스레드 "main"의 예외 javalangclasscastexception - 클래스 [b는 클래스 [c로 캐스트 될 수 없습니다 ([b 및 [c는 로더 'bootstrap'의 모듈 javabas
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- vue.js - axios를 사용하여 서버에 이미지를 업로드하는 방법
- python - 문자열에서 특정 문자 제거
다른 스레드를 사용하여 동시에 ArrayList에 항목을 추가하려고합니다. 동시 데이터 구조를 사용해야합니다 (또는 다른 목록을 사용하면 서로 병합합니다).
동시 목록에 대한 자세한 내용은 여기에서 찾을 수 있습니다. Java JDK에 동시 목록이 있습니까?