-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdd10be
commit ea81f40
Showing
10 changed files
with
621 additions
and
19 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
3.JavaMultithreading/src/com/javarush/task/task22/task2201/OurUncaughtExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.javarush.task.task22.task2201; | ||
|
||
public class OurUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { | ||
@Override | ||
public void uncaughtException(Thread t, Throwable e) { | ||
final String string = "%s : %s : %s"; | ||
if (Solution.FIRST_THREAD_NAME.equals(t.getName())) { | ||
System.out.println(getFormattedStringForFirstThread(t, e, string)); | ||
} else | ||
if (Solution.SECOND_THREAD_NAME.equals(t.getName())) { | ||
System.out.println(getFormattedStringForSecondThread(t, e, string)); | ||
} else { | ||
System.out.println(getFormattedStringForOtherThread(t, e, string)); | ||
} | ||
} | ||
|
||
protected String getFormattedStringForOtherThread(Thread t, Throwable e, String string) { | ||
return String.format(string,e.getClass().getSimpleName(),e.getCause(),t.getName()); | ||
} | ||
|
||
protected String getFormattedStringForSecondThread(Thread t, Throwable e, String string) { | ||
return String.format(string,e.getCause(),e.getClass().getSimpleName(),t.getName()); | ||
} | ||
|
||
protected String getFormattedStringForFirstThread(Thread t, Throwable e, String string) { | ||
return String.format(string,t.getName(),e.getClass().getSimpleName(),e.getCause()); | ||
} | ||
} | ||
|
48 changes: 48 additions & 0 deletions
48
3.JavaMultithreading/src/com/javarush/task/task22/task2201/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.javarush.task.task22.task2201; | ||
|
||
/* | ||
Строки нитей или строковые нити? Вот в чем вопрос | ||
*/ | ||
public class Solution { | ||
public static void main(String[] args) { | ||
new Solution(); | ||
} | ||
|
||
public static final String FIRST_THREAD_NAME = "1#"; | ||
public static final String SECOND_THREAD_NAME = "2#"; | ||
|
||
private Thread thread1; | ||
private Thread thread2; | ||
private Thread thread3; | ||
|
||
public Solution() { | ||
initThreads(); | ||
} | ||
|
||
protected void initThreads() { | ||
this.thread1 = new Thread(new Task(this, "A\tB\tC\tD\tE\tF\tG\tH\tI"), FIRST_THREAD_NAME); | ||
this.thread2 = new Thread(new Task(this, "J\tK\tL\tM\tN\tO\tP\tQ\tR\tS\tT\tU\tV\tW\tX\tY\tZ"), SECOND_THREAD_NAME); | ||
this.thread3 = new Thread(new Task(this, "\t\t"), "3#"); | ||
|
||
Thread.setDefaultUncaughtExceptionHandler(new OurUncaughtExceptionHandler()); | ||
|
||
this.thread1.start(); | ||
this.thread2.start(); | ||
this.thread3.start(); | ||
} | ||
|
||
public synchronized String getPartOfString(String string, String threadName) { | ||
String tab = "\t"; | ||
String result=""; | ||
try { | ||
result=string.substring(string.indexOf(tab)+1, string.lastIndexOf(tab)); | ||
} catch (Exception e) { | ||
if(threadName.equals(FIRST_THREAD_NAME)) throw new StringForFirstThreadTooShortException(); | ||
else if(threadName.equals(SECOND_THREAD_NAME)) throw new StringForSecondThreadTooShortException(); | ||
else throw new RuntimeException(); | ||
} | ||
return result; | ||
|
||
|
||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...hreading/src/com/javarush/task/task22/task2201/StringForFirstThreadTooShortException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.javarush.task.task22.task2201; | ||
|
||
public class StringForFirstThreadTooShortException extends RuntimeException { | ||
} |
4 changes: 4 additions & 0 deletions
4
...reading/src/com/javarush/task/task22/task2201/StringForSecondThreadTooShortException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.javarush.task.task22.task2201; | ||
|
||
public class StringForSecondThreadTooShortException extends RuntimeException { | ||
} |
20 changes: 20 additions & 0 deletions
20
3.JavaMultithreading/src/com/javarush/task/task22/task2201/Task.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.javarush.task.task22.task2201; | ||
|
||
public class Task implements Runnable { | ||
private String initialString; | ||
private Solution solution; | ||
|
||
public Task(Solution solution, String initialString) { | ||
this.solution = solution; | ||
this.initialString = initialString; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
String name = Thread.currentThread().getName(); | ||
String str = this.initialString; | ||
do { | ||
System.out.println(name + str); | ||
} while ((str = solution.getPartOfString(str, name)) != null || !str.isEmpty()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
3.JavaMultithreading/src/com/javarush/task/task22/task2201/Условие.jrtc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
taskKey="com.javarush.task.task22.task2201"\n\nСтроки нитей или строковые нити? Вот в чем вопрос | ||
|
||
1. Метод getPartOfString() должен возвращать подстроку между первой и последней табуляцией. | ||
2. На некорректные данные getPartOfString() должен бросить исключение: | ||
а) StringForFirstThreadTooShortException, если имя трэда FIRST_THREAD_NAME. | ||
б) StringForSecondThreadTooShortException, если имя трэда SECOND_THREAD_NAME. | ||
в) RuntimeException в других случаях. | ||
3. Реализуйте логику трех protected методов в OurUncaughtExceptionHandler используя вызовы соответствующих методов согласно следующим шаблонам: | ||
a) 1# : StringForFirstThreadTooShortException : java.lang.StringIndexOutOfBoundsException: String index out of range: -1 | ||
б) java.lang.StringIndexOutOfBoundsException: String index out of range: -1 : StringForSecondThreadTooShortException : 2# | ||
в) RuntimeException : java.lang.StringIndexOutOfBoundsException: String index out of range: -1 : 3# | ||
|
||
|
||
Требования: | ||
1. Метод getPartOfString() должен возвращать подстроку между первой и последней табуляцией строки string переданной ему в качестве первого параметра. | ||
2. В случае некорректных данных метод getPartOfString() должен бросить исключение StringForFirstThreadTooShortException, если имя трэда(threadName) Solution.FIRST_THREAD_NAME. | ||
3. В случае некорректных данных метод getPartOfString() должен бросить исключение StringForSecondThreadTooShortException, если имя трэда(threadName) Solution.SECOND_THREAD_NAME. | ||
4. В случае некорректных данных метод getPartOfString() должен бросить исключение RuntimeException, если имя трэда(threadName) не Solution.FIRST_THREAD_NAME или Solution.SECOND_THREAD_NAME. | ||
5. Метод getFormattedStringForFirstThread() должен возвращать строку сформированную из переданных параметров по шаблону указанному в задании. | ||
6. Метод getFormattedStringForSecondThread() должен возвращать строку сформированную из переданных параметров по шаблону указанному в задании. | ||
7. Метод getFormattedStringForOtherThread() должен возвращать строку сформированную из переданных параметров по шаблону указанному в задании. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
#Wed Aug 21 09:00:09 MSK 2019 | ||
javarush.last.task.key=com.javarush.task.task21.task2113.big16 | ||
#Wed Aug 21 16:25:10 MSK 2019 | ||
javarush.last.task.key=com.javarush.task.task22.task2201 | ||
javarush.session.id=7F47EE0E58271346345975044B4B1777 | ||
javarush.skip.set.autoscrolls=true | ||
javarush.user.secretkey=7e30b968-41a8-4d9c-998a-3ad4b6d9a79f | ||
javarush.query.filter= | ||
javarush.last.task.path=F\:/JavaRushTasks/3.JavaMultithreading/src/com/javarush/task/task21/task2113 | ||
javarush.last.task.path=F\:/JavaRushTasks/3.JavaMultithreading/src/com/javarush/task/task22/task2201 | ||
javarush.status.filter=ASSIGNED | ||
javarush.quest.filter=QUEST_JAVA_MULTITHREADING | ||
javarush.level.filter=-1 |
Oops, something went wrong.